ArrayList get() method to find class variable?

how to get values from arraylist in java
how to retrieve object values from arraylist in java
arraylist methods
how to get value from arraylist object in android
java arraylist get element by name
how to retrieve values from list object in java
arraylist get index
how to get multiple values from arraylist in java

So I have a list of a class type. For example, I have a ClassRoom class and a Student class.

I created an ArrayList of Students: ArrayList<Student> studList = new ArrayList<>();

I have a method to add a student, so index 0 is occupied:

    public void addStudent(int studID, String studName, int studPhoneNum){
        Student stud = new Student(studName, studID, studPhoneNum);

Now what I want is to search for a specific value inside the student's variables. So for example, a student is represented by a name, ID and phone number. How do I loop over that Student list to find a student with a specific phone number, then print him or his phone number?

This is the function:

    public void sendMessage(int phone){
        for (Student s : studList) {
            \\ then print the Student the phone number belongs to..

I do have a studPhoneNum variable set in the Student class.

The easiest way would be to create a function to get the phone number like this:

public int getPhoneNumber(int stuID){
    for (Student s: studList) {
        if( == stuID){
            return s.phoneNumber;
    return 0;

I'm supposing here that stuList is a global variable since you can access to it in addStudent without passing it as an argument, otherwise you'll have to pass it to the function. I hope it helps.

ArrayList get() method - Getting Element at Index, We will be using ArrayList.get() method to get the object at the specified index of the arraylist. 1. ArrayList get() method. ArrayList.get(int index) method returns the element at the specified position 'index' in the list. ArrayList;. import java.util. Arrays;. public class ArrayListExample Find me on Facebook and Twitter. Java.util.ArrayList.get() Method - The java.util.ArrayList.get(int index) method returns the element at the specified position in this list.

You can do as below:

  1. Using Java 8 stream:
            .filter(student -> student.getStudPhoneNum==studPhone)
            .ifPresent(student -> {
                // Do something
  2. Using for loop:

    for(Student s: studList){
            //do something

Java ArrayList get() Method example, ArrayList get(int index) method is used for fetching an element from the list. ArrayList; public class GetMethodExample { public static void main(String[] args) � The ArrayList.get () method is used to get the element of a specified position within the list. Package: java.util.

Since you are iterating over studList, 's' represents every individual student in that list.

public void sendMessage(int phone){
    for (Student s : studList) {
        if (s.getStudPhoneNum() == phone){

Here you would need to create getter methods (getStudPhoneNumber(), getStudName())to refer attributes of the Student class.

Java arraylist get method, Arraylist.get() method method. import java.util.*; public class test { public static void main(String[] args) { // create an empty array list with an� Java programming language provides List and ArrayList classes and objects in order to store multiple items in a single variable or structure. These List and ArrayList objects provide the get() method which is simply used to get an item from the List or ArrayList. get() Syntax. The get() method of the ArrayList or List has the following syntax. The index numbers in Lists and ArrayLists start from 0 which means the first item or element index number is 0.

Java.util.ArrayList.get() Method, get(int index) method returns the element at the specified position in this list. Declaration. Following is the declaration for java.util.ArrayList.get() method public E� ArrayList is an implementation of a java.util.List collection backed by a dynamic array.. This means three things. Firstly, you don't need to worry about resizing, allocating or coping elements into the array, the ArrayList will take care of it for you.

Java - The ArrayList Class, Java - The ArrayList Class - The ArrayList class extends AbstractList and implements After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Object get(int index) int size(). Returns the number of elements in this list. 16. Object[] toArray(). The get () method of ArrayList in Java is used to get the element of a specified index within the list.

Javanotes 8.1, Section 7.3 -- ArrayList, These classes contain the static methods Double.parseDouble() and Integer. parseInteger() that are used to convert strings to numerical values, and For example, an object of type Double contains a single instance variable, If you want to retrieve the double value that is wrapped in the� Java ArrayList. The ArrayList class is a resizable array, which can be found in the java.util package.. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).

  • if(s.getPhonenumber == phone)
  • Thank you! I can't believe I didn't find this earlier.
  • Thank you, I was missing the whole s.something thing. This works perfectly :)