Use ArrayList and a custom object instead.
public class MyStrings {
private String string1;
private String string2;
public MyStrings(String string1, String string2) {
this.string1 = string1;
this.string2 = string2;
}
public String getString1() {
return string1;
}
public String getString2() {
return string2;
}
}
Then to use it:
List<MyStrings> list = new ArrayList<MyStrings>();
list.add(new MyString("apple", "orange");
// To iterate:
for (MyStrings myStrings : list) {
System.out.println(myStrings.getString1());
System.out.println(myStrings.getString2());
}
Vector is no longer considered a good dynamic array in Java because of extra synchronization logic that's more or less useless, so it just adds unnecessary overhead. ArrayList is the go-to dynamic array. You could also use LinkedList, which is a doubly-linked list.
Of course, you should also use better naming conventions that describe what the values actually mean instead of the names that I used in my example.
ArrayList.. Apart, can you show some code you might have tried??ArrayList<Pair>instead.Pairis a class you define which has 2 fields.Pair-- write a class that actually describes what the two strings actually mean.