ArrayList.get()

ArrayList.get(int position) collect element at the input position from the ArrayList.


position : Required , Integer , Position from where element is to be returned.

Note : first position inside the ArrayList is 0 ( zero ).

Examples with output add()
ArrayList<String> languages = new ArrayList<String>(3);
languages.add("Java");
languages.add("PHP");
languages.add("Python");
System.out.println(languages); //[Java, PHP, Python]
System.out.println(languages.get(1)); // PHP
System.out.println(languages.get(0)); // Java
System.out.println(languages.get(2)); // Python
Example using integers
ArrayList<Integer> marks = new ArrayList<Integer>(3);
marks.add(55);
marks.add(54);
marks.add(60);
System.out.println(marks.get(2)); //60
We will get error message if input position is beyond the last element of the ArrayList.

Using size() we can get the number of elements present in an ArrayList.
ArrayList<Integer> marks = new ArrayList<Integer>(3);
	marks.add(55);
	marks.add(54);
	marks.add(60);
	for(int i=0; i < marks.size(); i++)
	System.out.println(marks.get(i)); 
Output
55
54
60
ArrayList Tutorials


Java

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer