How to get the last item of ArrayList in Java

2 Answers

0 votes
import java.util.ArrayList; 

public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>();

        al.add("java");
        al.add("c");
        al.add("php");
        al.add("python");
        al.add("c++");

        System.out.println(al.get(al.size()-1));
    }
}



/*
run:

c++

*/

 



answered Dec 4, 2019 by avibootz
0 votes
import java.util.ArrayList; 

public class MyClass {
    public static String get_last_item(ArrayList<String> al) {
        return al.get(al.size()-1); 
    }
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>();

        al.add("java");
        al.add("c");
        al.add("php");
        al.add("python");
        al.add("c++");

        System.out.println(get_last_item(al));
    }
}



/*
run:

c++

*/

 



answered Dec 4, 2019 by avibootz

Related questions

2 answers 168 views
1 answer 164 views
1 answer 121 views
1 answer 151 views
1 answer 161 views
161 views asked Jan 7, 2017 by avibootz
...