1

Conceptually what is going on here?

I have an ArrayList

ArrayList<SomeClass> myArray = new ArrayList<SomeClass>();

then I add something to it like this

myArray.add(new myArray(1, 2, 3));
5
  • possible duplicate of How to concatenate two arrays in Java? Commented May 23, 2014 at 19:38
  • myArray is an ArrayList Commented May 23, 2014 at 19:39
  • @JoelWitteveen that is not correct. The question was about the way in which an array list contains arrays Commented May 23, 2014 at 19:39
  • you should use something like myArray.addAll(Arrays.asList(1,2,3)) Commented May 23, 2014 at 19:40
  • new myArray(1, 2, 3) is not correct, how could you use new keyword to instantiate a not existing class myArray Commented May 23, 2014 at 19:42

4 Answers 4

3

I have an array

ArrayList myArray = new ArrayList();

No, you don't have an array. Arrays are declared using brackes [], like this:

int[] myArray;

You have an object of ArrayList class. This object will use an array internally to store the data and dynamically change the array for a newer one when needs to assign more values.

then I add something to it like this

myArray.add(new myArray(1, 2, 3));

Conceptually, this is wrong and won't even compile. If you want to add more than one value in your array, you should use ArrayList#addAll method, that receives another collection. Then, you have two ways to pass a Collection in one statement:

  1. Create a list using Arrays#asList:

    myArray.addAll(Arrays.asList(1, 2, 3));
    
  2. You can use double brace initialization:

    myArray.addAll(new ArrayList() {{
            add(1);
            add(2);
            add(3);
        }});
    

You should take into account that you should specify the generic for your ArrayList instead of using raw data:

ArrayList<Integer> myArray = new ArrayList<Integer>();
myArray.addAll(new ArrayList<Integer>() {{
        add(1);
        add(2);
        add(3);
    }});

More info:


Note that if you have an ArrayList that is tied to a custom class, let's say ArrayList<SomeClass> myArrayList, then the compiler prevents you to add elements to the list that doesn't pass the IS-A test (noted by instanceof operator), so this code:

ArrayList<SomeClass> myArrayList = new ArrayList<SomeClass>();
myArrayList..addAll(new ArrayList<Integer>() {{
        add(1);
        add(2);
        add(3);
    }});

Won't be able to compile since Integer is not a SomeClass. Similar with this piece of code:

ArrayList<SomeClass> myArrayList = new ArrayList<SomeClass>();
myArray.add(Arrays.asList(1, 2, 3));

But due to type erasure you can trick the compiler by using raw types. For example:

public void addData(ArrayList yourArrayList, Object anyData) {
    yourArrayList.add(anyData);
}

Then in your code, you call this method:

ArrayList<String> myArrayList = new ArrayList<String>(); //allows Strings only
myArrayList.add("Hello world");
//myArrayList.add(0); //this won't compile because trying to add an Integer
addData(myArrayList, 0); //this will compile for using raw ArrayList and sending an Integer
System.out.println(myArrayList);

It will print:

[Hello world, 0]
Sign up to request clarification or add additional context in comments.

8 Comments

"Conceptually what is going on here?"
if you know the size of the added arraylist, it would be nice the define the size of the arraylist : new ArrayList(size)
@LuiggiMendoza He seems to be asking what an arraylist is / how it works. Nothing else.
@JordanD to begin with, that code doesn't compile nor even work.
@LuiggiMendoza , he did not ask to make it work. He wants to know what it means to have an arraylist
|
0
myArray.add(new myArray(1, 2, 3));

This is not correct and don't compile, cause myArray is not a type, it's just a reference, you need something of type integer or if you want to add another list, then use addAll.

You could use something like this.

List<Integer> myArray = new ArrayList<>(); // specify with generics type parameter
//do some things and then
myArray.addAll(Arrays.asList(1,2,3)); // use this helper method to add a list to another list

Or if you want to only create and populate it, just use

List<Integer> myArray = Arrays.asList(1,2,3,4,5);

1 Comment

The user asked: "Conceptually what is going on here?"
0

I think I know what you're trying to do, say you have this.

ArrayList<Integer[]> myArray = new ArrayList<Integer[]>();

and you wanted to add an array to it, you're basically going to call

myArray.add(((Integer[]) new int[] { 0, 1, 2 }));

Which will add the array [0, 1, 2] to the next index in the ArrayList.

For exmaple, the following code

ArrayList<Integer[]> myArray = new ArrayList<Integer[]>();
myArray.add(((Integer[]) new int[] { 0, 1, 2 }));
myArray.add(((Integer[]) new int[] { 1, 0, 2 }));
myArray.add(((Integer[]) new int[] { 3, 2, 1 }));

Would create the following results:

myArray index 0 = [0, 1, 2]
myArray index 1 = [1, 0, 2]
myArray index 2 = [3, 2, 1]

Is this your question?

Comments

0

You don't actually have an array, you have an ArrayList. An ArrayList is a resizable implementation of the List interface. This means, among other things, that you don't have to define a length like you would with an array.

I don't think the code you have provided will actually compile because when you go:

myArray.add(new myArray(1, 2, 3));

you haven't provided a type. You should go something like:

myArray.add(new ArrayList<Integer>(Arrays.asList(1,2,3)));

Using asList() will give almost the equivalent of assigning "default" values to an array. The array equivalent would look something like this:

Integer[] intArray =  new Integer[] {1,2,3};

You should also parameterize your ArrayList(s), this basically means you are saying what that ArrayList can hold. For instance:

ArrayList<Integer> 

Can hold Integers.

The reason you must use a wrapper class (Integer & not int) is because Collections in Java don't permit you to store primitive types such as int or double.

You can have a Collection holding another Collection. In this case you could go:

ArrayList<ArrayList<Integer>> myArrayList = new ArrayList<ArrayList<Integer>>();
    myArrayList.add(new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
    myArrayList.add(new ArrayList<Integer>(Arrays.asList(9, 12, 24)));
    for (ArrayList<Integer> index : myArrayList) {
        System.out.println(index);
    }

Here you have an ArrayList holding two other ArrayLists. This results in the printing of:

[1, 2, 3]
[9, 12, 24]

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.