📜  在Java中反转 ArrayList

📅  最后修改于: 2022-05-13 01:54:32.801000             🧑  作者: Mango

在Java中反转 ArrayList

假设你已经通过Java中的 arraylist 并了解 arraylist。这篇文章包含反转数组列表的不同示例,如下所示:
1. 通过编写我们自己的函数(使用额外的空间): RevArrayList 类中的 reverseArrayList() 方法包含使用整数对象反转数组列表的逻辑。该方法以一个arraylist为参数,逆序遍历并将所有元素添加到新创建的arraylist中。最后返回反转的arraylist。

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 reverseArrayList(ArrayList alist)
    {
        // Arraylist for storing reversed elements
        ArrayList revArrayList = new ArrayList();
        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 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 arrayli = new ArrayList();
 
        // 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);
    }
}


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 reverseArrayList(ArrayList 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 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 arrayli = new ArrayList();
 
        // 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);
    }
}


Java
// Java program for reversing an arraylist
import java.io.*;
import java.util.*;
 
public class GFG2 {
    public static void main(String[] args)
    {
        // Declaring arraylist without any initial size
        ArrayList arrayli = new ArrayList();
 
        // Appending elements at the end of the list
        arrayli.add(new Integer(9));
        arrayli.add(new Integer(145));
        arrayli.add(new Integer(878));
        arrayli.add(new Integer(343));
        arrayli.add(new Integer(5));
        System.out.print("Elements before reversing: ");
        printElements(arrayli);
 
        // Collections.reverse method takes a list as a
        // parameter and reverses the passed parameter
      //(no new array list is required)
        Collections.reverse(arrayli);
        System.out.print("\nElements after reversing: ");
        printElements(arrayli);
    }
 
    // Iterate through all the elements and print
    public static void printElements(ArrayList alist)
    {
        for (int i = 0; i < alist.size(); i++) {
            System.out.print(alist.get(i) + " ");
        }
    }
}


Java
// Java program for reversing an arraylist
import java.io.*;
import java.util.*;
class Employee {
    int empID;
    String empName;
    String deptName;
 
    // Constructor for initializing the class variables
    public Employee(int empID, String empName, String deptName)
    {
        this.empID = empID;
        this.empName = empName;
        this.deptName = deptName;
    }
}
 
public class GFG3 {
    public static void main(String[] args)
    {
        // Declaring arraylist without any initial size
        ArrayList arrayli = new ArrayList();
 
        // Creating user defined objects
        Employee emp1 = new Employee(123, "Rama", "Facilities");
        Employee emp2 = new Employee(124, "Lakshman", "Transport");
        Employee emp3 = new Employee(125, "Ravan", "Packing");
 
        // Appending all the objects for arraylist
        arrayli.add(emp1);
        arrayli.add(emp2);
        arrayli.add(emp3);
 
        System.out.print("Elements before reversing: ");
        printElements(arrayli);
 
        // Collections.reverse method takes a list as a
        // parameter and reverse the list
        Collections.reverse(arrayli);
        System.out.print("\nElements after reversing: ");
        printElements(arrayli);
    }
 
    // Iterate through all the elements and print
    public static void printElements(ArrayList alist)
    {
        for (int i = 0; i < alist.size(); i++) {
            System.out.print("\n EmpID:" + alist.get(i).empID + 
            ", EmpName:" + alist.get(i).empName + ", Department:" +
                                          alist.get(i).deptName);
        }
    }
}


输出:
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. 循环终止后返回arraylist。

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 reverseArrayList(ArrayList 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 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 arrayli = new ArrayList();
 
        // 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);
    }
}
输出:
Elements before reversing: 12 13 123 54 1 
Elements after reversing: 1 54 123 13 12

2. 使用 Collections 类: Collections 是Java.util 包中的一个类,其中包含各种静态方法,用于搜索、排序、反转、查找最大值、最小值……等。我们可以使用 In-built Collections.reverse() 方法来反转数组列表。它将一个列表作为输入参数并返回反向列表。

Java

// Java program for reversing an arraylist
import java.io.*;
import java.util.*;
 
public class GFG2 {
    public static void main(String[] args)
    {
        // Declaring arraylist without any initial size
        ArrayList arrayli = new ArrayList();
 
        // Appending elements at the end of the list
        arrayli.add(new Integer(9));
        arrayli.add(new Integer(145));
        arrayli.add(new Integer(878));
        arrayli.add(new Integer(343));
        arrayli.add(new Integer(5));
        System.out.print("Elements before reversing: ");
        printElements(arrayli);
 
        // Collections.reverse method takes a list as a
        // parameter and reverses the passed parameter
      //(no new array list is required)
        Collections.reverse(arrayli);
        System.out.print("\nElements after reversing: ");
        printElements(arrayli);
    }
 
    // Iterate through all the elements and print
    public static void printElements(ArrayList alist)
    {
        for (int i = 0; i < alist.size(); i++) {
            System.out.print(alist.get(i) + " ");
        }
    }
}
输出:
Elements before reversing: 9 145 878 343 5 
Elements after reversing: 5 343 878 145 9

3.反转用户定义对象的arraylist:创建一个Employee类,用于创建用户定义的对象,其中employeeID,employeeName,departmentName作为类变量,在构造函数中初始化。创建了一个仅接受员工(用户定义)对象的数组列表。使用 add() 方法将这些对象添加到数组列表中。使用 Collections 类的内置 reverse() 方法反转 arraylist。
printElements() 静态方法仅用于避免在程序中多写一个类。

Java

// Java program for reversing an arraylist
import java.io.*;
import java.util.*;
class Employee {
    int empID;
    String empName;
    String deptName;
 
    // Constructor for initializing the class variables
    public Employee(int empID, String empName, String deptName)
    {
        this.empID = empID;
        this.empName = empName;
        this.deptName = deptName;
    }
}
 
public class GFG3 {
    public static void main(String[] args)
    {
        // Declaring arraylist without any initial size
        ArrayList arrayli = new ArrayList();
 
        // Creating user defined objects
        Employee emp1 = new Employee(123, "Rama", "Facilities");
        Employee emp2 = new Employee(124, "Lakshman", "Transport");
        Employee emp3 = new Employee(125, "Ravan", "Packing");
 
        // Appending all the objects for arraylist
        arrayli.add(emp1);
        arrayli.add(emp2);
        arrayli.add(emp3);
 
        System.out.print("Elements before reversing: ");
        printElements(arrayli);
 
        // Collections.reverse method takes a list as a
        // parameter and reverse the list
        Collections.reverse(arrayli);
        System.out.print("\nElements after reversing: ");
        printElements(arrayli);
    }
 
    // Iterate through all the elements and print
    public static void printElements(ArrayList alist)
    {
        for (int i = 0; i < alist.size(); i++) {
            System.out.print("\n EmpID:" + alist.get(i).empID + 
            ", EmpName:" + alist.get(i).empName + ", Department:" +
                                          alist.get(i).deptName);
        }
    }
}
输出:
Elements before reversing: 
 EmpID:123, EmpName:Rama, Department:Facilities
 EmpID:124, EmpName:Lakshman, Department:Transport
 EmpID:125, EmpName:Ravan, Department:Packing
Elements after reversing: 
 EmpID:125, EmpName:Ravan, Department:Packing
 EmpID:124, EmpName:Lakshman, Department:Transport
 EmpID:123, EmpName:Rama, Department:Facilities