1

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.

4
  • Where in this code is a linked list.. im confused Commented May 8, 2016 at 2:44
  • sorry it is an ArrayList. Commented May 8, 2016 at 2:48
  • You want to invoke methods of the objects stored in your arraylist while you loop through the mentioned arraylist? Commented May 8, 2016 at 2:53
  • Yes. I will need to go back either in a loop, invoke the methods, get and set values, or I will need to get to specific indices to do the same thing one at a time. Commented May 8, 2016 at 2:56

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.