This is my code I have so far but I don't know how to access an element in the Array myList?
And is the inex of an array list start at 0 or 1? I have just found out about array lists and i need a few pointers and some help
ArrayList<String> myList = new ArrayList<String>();
myList.add("hello");
myList.add("5");
myList.add("3");
myList.add("8");
int totalElements = myList.size();
System.out.println(totalElements);
private String[] myList;
public String getList() {
return this.myList[0];
All you have to do is:
myList.get(Index);
This would return you the type of Object you used while creating the ArrayList. In your case it will return a String. Hence, what you can do is:
String firstElement = myList.get(0); //This would return "Hello"
This also shows that ArrayList indices start with 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With