0

I am modifying my question to make it more understandable. I have the following arraylist

ArrayList<AnotherClass> exmapleName = new ArrayList<AnotherClass>();
// AnotherClass is a class I created, I store objects of the class in this array, the code to this doesn't matter this works.

Inside of exampleName I have different types of data

public AnotherClass (String someString,  int someInt, int anotherInt, int[] intArray, ArrayList<Integer> arrayList)
//Header of the constructor

Now what I need to do is access and store arrayList[0]. But I need to do it once the object has been created so there is actually something inside arrayList.

This is what I've tried but doesn't seem to work

         AnotherClass test1 = exampleName.get(1);
        ArrayList<Integer> test2 = test1.arrayList;
        int test3 = test2.arrayList[0];
// I broke it down into separate lines to make it more understandable 

compiler errors are as follows

cannot find symbol
                    int test3 = test2.arrayList[0];
symbol:   variable arrayList
location: variable test2 of type ArrayList<Integer>

1 error

1
  • You have an ArrayList of int[]? It is this data your type.@Blueaddiction Commented Dec 24, 2015 at 6:59

2 Answers 2

2

If you're having a "normal" array, you can simply access the i-th element, with the index, as myArrayList.get(1).normalArray[i].

If you need to process the array, consider storing it into a local copy and access from it.

int[] numbers = myArrayList.get(1);
for(Integer num : numbers){
  // Process the numbers array.
  System.out.print(num);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this before but, I need to store the variable as an int but it requires an integer array R.java:70: error: array required, but ArrayList<Integer> found
@Blueaddiction So you're saying that normalArray is declared as an ArrayList<Integer>? You didn't say that in your original post. You said "int array" which everybody else has misinterpreted. Please show us more of the code.
I said the main array is an integer arraylist which inside has an int array. I'll post some more code when I go back to it taking a break
I've modified the main post to reflect the problem more clearly
0
 int arr[] = myArrayList.get(1);

access like,

int first=arr[0];

2 Comments

actually wait it didnt work, error: incompatible types: //my other class -->(anotherClass) cannot be converted to int[] int[] test1 = eQuestion.get(1);
can you please post some more code so that I can figure out if its still not working for you.

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.