2

What I am trying to do is add an array to an arraylist. I've looked at other examples that tell me to do what I am doing, but I get nonsense output when I run the program. I would also like to access certain elements of an array within the arraylist, and I have no idea how to do that.

static int elements = 10;  //Or whatever number you'd like

public static void main(String[] args)
    {
        int[] person = new int[4];
        ArrayList personID = new ArrayList();

        experiment(personID, person);
    }

private static void experiment(ArrayList personID, int[] person)
    {
        for(int i = 0; i < elements; i++)
            {
                person[0] = i;
                person[1] = i;
                person[2] = i;
                person[3] = i;

                personID.add(person);
            }
        System.out.print(personID);
    }

Output:

[[I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87, [I@e1cba87]

Not that much of an explanation is needed, but I declare the array and arraylist, pass them to a function which keeps giving the array's elements different values and then, supposedly, adding the array itself to the arraylist for each iteration.

The output, though, it nothing like what I am looking for. I could do another loop to print each array element for each spot along the array list, but I don't know how to do that. I figured

System.out.print(personID.get(i[0]);

Or

System.out.print(personID.get(i)[0];

But it doesn't work and I'm lost...

Thank you for dealing with me!

  • 1
    [How to print an array in Java](http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java) – Obicere Mar 24 '14 at 03:24

3 Answers3

1

It's just a problem with the way you're printing the arrays. Try this instead:

for (int[] a : personID)
    System.out.println(Arrays.toString(a));
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

Arrays doesn't implement its own toString() implementation it uses Object#toString() and that is why you are watching that output

getClass().getName() + '@' + Integer.toHexString(hashCode())

[I means that is an Integer array plus @ followed with it's hashcode e1cba87.

One way to solve it is using Arrays.toString()

For example:

public static void main(String[] args){
        int[] person = new int[4];
        List<int[]> personID = new ArrayList<>();
        experiment(personID, person);


       //now print
       for(int[] array : personID){
          System.out.println(Arrays.toString(array));
       }
}
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • So, you mean like this? `System.out.println(Arrays.toString(personID));` It tells me that it cannot convert the ArrayList to whatever array. – user3453876 Mar 24 '14 at 03:51
  • @user3453876 `personID` not. `Arrays.toString()` is clear you have to pass `person` – nachokk Mar 24 '14 at 03:53
  • @user3453876 i edit and put an example based in your code, try to use `generics` add stability to your code by making more of your bugs detectable at compile time – nachokk Mar 24 '14 at 03:59
  • That worked great. :D I'm going to have to look into lists, apparently. So, what about picking out a specific element of one of those arrays? – user3453876 Mar 24 '14 at 04:04
  • @user3453876 just array[0] or array[1] .. array[n-1] – nachokk Mar 24 '14 at 04:12
  • I mean from the ArrayList. `personID.get(i[0])` ? – user3453876 Mar 24 '14 at 04:16
  • @user3453876 personID.get(0)[0], will be, having an `ArrayList` is like having a bidimensional array – nachokk Mar 24 '14 at 04:18
0

Not only are you attempting to print arrays by implicitly invoking toString, but also adding the same array over and over to the list. The variable called person is a reference to an array object. You keep changing its elements and adding the same reference.

Were your print routine to be correct, the output would only show an array of four 10s repeated 10 times.

Judge Mental
  • 5,209
  • 17
  • 22
  • That's fine. This is just for me to understand how to implement this idea into something else. So, the four 10s repeated doesn't matter much to me. Also, my understanding is that the elements ArrayList don't refer back to the array, but instead copies their values over, so changing the elements of the array doesn't do anything but change what the next element of the ArrayList will have in it. – user3453876 Mar 24 '14 at 03:59
  • I take that back, it does seem to just save the reference to the array instead of the values. Hmm... – user3453876 Mar 24 '14 at 04:10