To find an element matching specific criteria in a given list, we: invoke stream() on the list. call the filter() method with a proper Predicate. call the findAny() construct, which returns the first element that matches the filter predicate wrapped in an Optional if such an element exists.
Return an ArrayList From a Non-Static Function in Java This function is non-static, so an object of the class will be needed to invoke it. In the following code, we create such a function. The function myNumbers() is not static. So, we need to create an instance of ClassB in ClassA .
As many have already told you:
mainList.get(3);
Be sure to check the ArrayList Javadoc.
Also, be careful with the arrays indices: in Java, the first element is at index 0
. So if you are trying to get the third element, your solution would be mainList.get(2);
Time to familiarize yourself with the ArrayList
API and more:
ArrayList
at Java 6 API Documentation
For your immediate question:
mainList.get(3);
mainList.get(list_index)
mainList.get(3);
For future reference, you should refer to the Java API for these types of questions:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html
It's a useful thing!
We print the value using mainList.get(index) where index starts with '0'. For Example: mainList.get(2) prints the 3rd element in the list.
You can simply get your answer from ArrayList API doc.
Please always refer API documentation .. it helps
Your call will looklike following :
mainList.get(3);
Here is simple tutorial for understanding ArrayList with Basics :) :
http://www.javadeveloper.co.in/java/java-arraylist-tutorial.html
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