反转列表的Java程序
反转列表是指在反转列表后,第一个元素应与最后一个元素交换,第二个元素应与倒数第二个元素交换,依此类推,直到中间元素和结果列表为反转列表。
例子:
Input:
"PLATFORM", "LEARNING", "BEST", "THE", "IS", "GFG"
Output:
Reverse order of given List :-
[GFG, IS, THE, BEST, LEARNING, PLATFORM]
我们主要可以通过三种方式来反转列表:
- 递归地
- 使用 Collections.reverse()
- 使用 List.add() 和 List.remove 方法
方法一:使用递归
函数直接或间接调用自身的过程称为递归,对应的函数称为递归函数。
Java
// Java Program to Reverse a List recursively
import java.io.*;
import java.util.*;
class GFG {
public static void revlist(List list)
{
// base condition when the list size is 0
if (list.size() <= 1 || list == null)
return;
T value = list.remove(0);
// call the recursive function to reverse
// the list after removing the first element
revlist(list);
// now after the rest of the list has been
// reversed by the upper recursive call,
// add the first value at the end
list.add(value);
}
public static void main(String[] args)
{
System.out.println(
"Reverse order of given List :- ");
List gfg = new ArrayList<>(
Arrays.asList("PLATFORM", "LEARNING", "BEST", "THE", "IS", "GFG"));
revlist(gfg);
System.out.println(gfg);
}
}
Java
// Java program to reverse the list
// using Collections.reverse() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List number = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println(
"Reverse order of given List :- ");
// the number list will be reversed using this method
Collections.reverse(number);
System.out.println(number);
}
}
Java
// Java program to reverse the list usinf List.add()
// and List.remove() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List number = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println(
"Reverse order of given List :- ");
for (int k = 0, j = number.size() - 1; k < j; k++)
{
number.add(k, number.remove(j));
}
System.out.println(number);
}
}
输出
Reverse order of given List :-
[GFG, IS, THE, BEST, LEARNING, PLATFORM]
方法 2:使用Collections.reverse()
Java.util.Collections.reverse() 方法是一个Java.util.Collections 类方法。它反转作为参数传递的列表中元素的顺序。
Java
// Java program to reverse the list
// using Collections.reverse() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List number = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println(
"Reverse order of given List :- ");
// the number list will be reversed using this method
Collections.reverse(number);
System.out.println(number);
}
}
输出
Reverse order of given List :-
[8, 7, 6, 5, 4, 3, 2, 1]
方法三:使用List.add() + List.remove()
List 接口的 List.add() 方法用于将参数中的指定元素追加到列表的末尾。
List 接口的 List.remove() 方法用于从列表中删除参数中的指定元素。
Java
// Java program to reverse the list usinf List.add()
// and List.remove() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List number = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println(
"Reverse order of given List :- ");
for (int k = 0, j = number.size() - 1; k < j; k++)
{
number.add(k, number.remove(j));
}
System.out.println(number);
}
}
输出
Reverse order of given List :-
[8, 7, 6, 5, 4, 3, 2, 1]