在 Java 中反转数组列表
原文:https://www.geeksforgeeks.org/reverse-an-arraylist-in-java/
Java 语言(一种计算机语言,尤用于创建网站)
// Java program for reversing an arraylist
import java.io.*;
import java.util.*;
class RevArrayList {
// Takes an arraylist as a parameter and returns
// a reversed arraylist
public ArrayList<Integer> reverseArrayList(ArrayList<Integer> alist)
{
// Arraylist for storing reversed elements
ArrayList<Integer> revArrayList = new ArrayList<Integer>();
for (int i = alist.size() - 1; i >= 0; i--) {
// Append the elements in reverse order
revArrayList.add(alist.get(i));
}
// Return the reversed arraylist
return revArrayList;
}
// Iterate through all the elements and print
public void printElements(ArrayList<Integer> alist)
{
for (int i = 0; i < alist.size(); i++) {
System.out.print(alist.get(i) + " ");
}
}
}
public class GFG {
public static void main(String[] args)
{
RevArrayList obj = new RevArrayList();
// Declaring arraylist without any initial size
ArrayList<Integer> arrayli = new ArrayList<Integer>();
// Appending elements at the end of the list
arrayli.add(new Integer(1));
arrayli.add(new Integer(2));
arrayli.add(new Integer(3));
arrayli.add(new Integer(4));
System.out.print("Elements before reversing:");
obj.printElements(arrayli);
arrayli = obj.reverseArrayList(arrayli);
System.out.print("\nElements after reversing:");
obj.printElements(arrayli);
}
}
Output:
Elements before reversing:1 2 3 4
Elements after reversing:4 3 2 1
1。通过编写我们自己的函数(而不使用额外的空间 ): 在前面的例子中,数组列表被额外用于存储所有占用更多空间的反向元素。为了避免这种情况,可以使用相同的数组列表进行反转。 逻辑: 1。循环 n/2 次,其中“n”是数组列表中的元素数。 2。第一遍,交换第一个和第 n 个元素 3。在第二遍中,交换第二个和第(n-1)个元素,以此类推,直到到达数组列表的中间。 4。循环结束后返回数组列表。
Java 语言(一种计算机语言,尤用于创建网站)
// Java program for reversing an arraylist
import java.io.*;
import java.util.*;
class RevArrayList {
// Takes an arraylist as a parameter and returns
// a reversed arraylist
public ArrayList<Integer> reverseArrayList(ArrayList<Integer> alist)
{
// Arraylist for storing reversed elements
// this.revArrayList = alist;
for (int i = 0; i < alist.size() / 2; i++) {
Integer temp = alist.get(i);
alist.set(i, alist.get(alist.size() - i - 1));
alist.set(alist.size() - i - 1, temp);
}
// Return the reversed arraylist
return alist;
}
// Iterate through all the elements and print
public void printElements(ArrayList<Integer> alist)
{
for (int i = 0; i < alist.size(); i++) {
System.out.print(alist.get(i) + " ");
}
}
}
public class GFG1 {
public static void main(String[] args)
{
RevArrayList obj = new RevArrayList();
// Declaring arraylist without any initial size
ArrayList<Integer> arrayli = new ArrayList<Integer>();
// Appending elements at the end of the list
arrayli.add(new Integer(12));
arrayli.add(new Integer(13));
arrayli.add(new Integer(123));
arrayli.add(new Integer(54));
arrayli.add(new Integer(1));
System.out.print("Elements before reversing: ");
obj.printElements(arrayli);
arrayli = obj.reverseArrayList(arrayli);
System.out.print("\nElements after reversing: ");
obj.printElements(arrayli);
}
}
Output:
Elements before reversing: 12 13 123 54 1
Elements after reversing: 1 54 123 13 12
版权属于:月萌API www.moonapi.com,转载请注明出处