Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would use a get() method to access an element in an arrayList? [closed]

Tags:

java

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];
like image 463
user1254044 Avatar asked Dec 01 '25 05:12

user1254044


1 Answers

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

like image 140
noMAD Avatar answered Dec 03 '25 22:12

noMAD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!