Java向量的所有元素的逆序
Vector 类实现动态数组意味着它可以根据需要缩小和扩展其大小,只是可能具有与数组中相同的操作。不要将它与 ArrayList 混淆,因为向量和 ArrayList 之间有一条细线,其中向量是同步的,其余插入顺序在两者中保持相同。 Vector 存在于Java.util包中并实现了列表接口
插图:反转向量中元素的顺序
Input : [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Input : [“GEEKS”, “FOR”, “geeks”]
Output: [“geeks”, “FOR”, “GEEKS”]
方法:
- 使用 for 循环(朴素的方法)
- 使用Collection.reverse()方法
- 使用listIterator()方法
方法一:使用for循环逆序打印vector的元素。
Java
// Java program to print vector element
// in reverse using for loop
// Importing Vector, Collection & ListIterator classes
// and generic java input output class
import java.io.*;
import java.util.Vector;
import java.util.Collections;
import java.util.ListIterator;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating vector of integer
Vector v1 = new Vector();
// Adding element to vector
// Custom inputs
v1.add(1);
v1.add(2);
v1.add(3);
v1.add(4);
v1.add(5);
// Display message
System.out.println("Before reverse of vector : ");
// Printing all elements of vector before reversing
System.out.println(v1);
// Display message
System.out.println("After reverse of vector : ");
// Iterating from last index of vector to 0
// index = vector.size()-1 (last index)
for (int i = v1.size() - 1; i >= 0; i--) {
// Printing elements of vector after reversing
System.out.println(v1.get(i));
}
}
}
Java
// Java Program to reverse order of elements of vector
// Importing generic java input/output classes
import java.io.*;
// Importing Vector and Collection class
// from java.util package
import java.util.Vector;
import java.util.Collections;
// Main Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create a Vector object
Vector v = new Vector();
// Add elements to Vector
v.add("GFG");
v.add("EarlierGreen");
v.add("NowBlack");
// Display vector element before reversing
System.out.println(
"Before Reverse Order, Vector Contains : " + v);
// reverse() method to reverse vector element
// by passing vector object so as to reverse
Collections.reverse(v);
// Display vector element after reversing
System.out.println(
"After Reverse Order, Vector Contains : " + v);
}
}
Java
// Java Program to reverse order of elements of vector
// using listiterator
// Importing Vector and ListIterator classes
// of java.util package
import java.util.Vector;
import java.util.ListIterator;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create(Declare) empty vector
Vector v1 = new Vector();
// Add elements to vector using add() method
v1.add("Geeks");
v1.add("for");
v1.add("Geeks");
v1.add("is");
v1.add("Best");
// Print message
System.out.print("Before: ");
// Printing all elements of Vector before reversing
System.out.println(v1);
// Declare list iterator
ListIterator l_itr
= v1.listIterator(v1.size());
// Iistiterator to reverse the vector element using
// hashPrevious() method
// Print message
System.out.println("After: ");
while (l_itr.hasPrevious())
// Print vector elements after reversing
System.out.println(l_itr.previous());
}
}
输出
Before reverse of vector :
[1, 2, 3, 4, 5]
After reverse of vector :
5
4
3
2
1
时间复杂度: O(n) of n 次,其中 n 是向量中的元素数量。
方法二: Collection.reverse()方法顾名思义就是Collection类的一个方法。
句法:
Collections.reverse(vector) ;
参数:要反转的矢量对象
返回值:它返回向量元素的反向。
Java
// Java Program to reverse order of elements of vector
// Importing generic java input/output classes
import java.io.*;
// Importing Vector and Collection class
// from java.util package
import java.util.Vector;
import java.util.Collections;
// Main Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create a Vector object
Vector v = new Vector();
// Add elements to Vector
v.add("GFG");
v.add("EarlierGreen");
v.add("NowBlack");
// Display vector element before reversing
System.out.println(
"Before Reverse Order, Vector Contains : " + v);
// reverse() method to reverse vector element
// by passing vector object so as to reverse
Collections.reverse(v);
// Display vector element after reversing
System.out.println(
"After Reverse Order, Vector Contains : " + v);
}
}
输出
Before Reverse Order, Vector Contains : [GFG, EarlierGreen, NowBlack]
After Reverse Order, Vector Contains : [NowBlack, EarlierGreen, GFG]
方法 3:使用listIterator()方法
句法:
public ListIterator listIterator()
参数:此方法不接受输入参数。
返回值:此方法返回一个 ListIterator 对象,该对象可用于遍历 Vector 对象。此对象可用于遍历 Vector 对象。它是双向的,因此可以分别使用next()和previous()进行向前和向后遍历。
返回类型: ListIterator
Java
// Java Program to reverse order of elements of vector
// using listiterator
// Importing Vector and ListIterator classes
// of java.util package
import java.util.Vector;
import java.util.ListIterator;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create(Declare) empty vector
Vector v1 = new Vector();
// Add elements to vector using add() method
v1.add("Geeks");
v1.add("for");
v1.add("Geeks");
v1.add("is");
v1.add("Best");
// Print message
System.out.print("Before: ");
// Printing all elements of Vector before reversing
System.out.println(v1);
// Declare list iterator
ListIterator l_itr
= v1.listIterator(v1.size());
// Iistiterator to reverse the vector element using
// hashPrevious() method
// Print message
System.out.println("After: ");
while (l_itr.hasPrevious())
// Print vector elements after reversing
System.out.println(l_itr.previous());
}
}
输出
Before: [Geeks, for, Geeks, is, Best]
After:
Best
is
Geeks
for
Geeks