简体   繁体   中英

Accessing methods of objects stored in an ArrayList in Java

I am trying to access methods and values of an object in a ArrayList. I have a system that works but an wondering if there is a better way of doing it. I have simplified the code to illustrate what I am doing. In the real code I will have hundreds of "Test" objects and would be able to loop through them by passing the index to the list.get() method. I feel there should be a better way of accessing the methods than creating a temporary Test object to get to them.
Something like:

list.the object at the list's index.theObjectsMethod(pass a value)

Instead of what I did:

import java.util.ArrayList;

public class Test {
static ArrayList<Object> list = new ArrayList<>();
private int index;
private int value;

public Test(int index, int value) {
    this.index = index;
    this.value = value;
}
public int add(int x, int y) {
    value = x + y;
    return value;
}
public int subtract(int x, int y) {
    return x - y;
}

public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
        Test theTest = new Test(i, i + 1);
        list.add(i, theTest);
    }
/*
 * my way of accessing the methods
*/
    Test tempTest = (Test) list.get(0);
    tempTest.add(12, 1);
    System.out.println(tempTest.value);
    Test tTest = (Test) list.get(1);
    System.out.println(tTest.value);
  }
 }

I used a arrayList because I needed to loop thru the structure, and access specific index locations.

You have created a List of Object . Therefore, any iteration will only provide access to the methods of the Object class. You can:

  1. Change the List declaration to List<Test> list = new ArrayList<>();

This approach would allow you to access the value without casing it as in the code you posted. For example:

for (Test test : list) {
  test.add(1, 2);
}

Or:

Test t = list.get(0);  //though watch out for empty list, etc.
t.subtract(2, 1);
  1. You can continue to cast your retrieval, but it is not an optimal approach.

As an aside, when adding to a list the default it to append, so you can simply do something like to add to the list.

for (int i = 0; i < 5; ++i) {
  Test t = new Test(i, i+1);
  list.add(t);
}

Every your Test object have its index, so I think you only add a Test object to list by call list.add(theTest); . To get a Test object in list by their index, you can Override the get method of list . I think this can help you:

import java.util.ArrayList;

public class Test {

    private final int index;
    private int value;

    public Test(int index, int value) {
        this.index = index;
        this.value = value;
    }

    public int add(int x, int y) {
        value = x + y;
        return value;
    }

    public int subtract(int x, int y) {
        return x - y;
    }

    public int getIndex() {
        return this.index;
    }

    public static void main(String[] args) {
        ArrayList<Test> list = new ArrayList<Test>() {
            @Override
            public Test get(int index) {
                for(Test theTest : this) {
                    if(theTest.getIndex()==index){
                        return theTest;
                    }
                }
                return null;
            }
        };
        for (int i = 0; i < 5; i++) {
            Test theTest = new Test(i, i + 1);
            list.add(theTest);
        }
        /**
        ** my way of accessing the methods
        **/
        Test tempTest = list.get(0);
        tempTest.add(12, 1);
        System.out.println(tempTest.value);
        Test tTest = list.get(1);
        System.out.println(tTest.value);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM