views:

86

answers:

5

I've found plenty of entries here that say

someFunc(new int[]{1,2,3});

works for calling methods and being used in a for/each loop.

//======

How about for assignment into a collection?

I've tried this:

ArrayList<int[]> data = new ArrayList<int[]>();
data.add(new int[]{21, 19629});

and I get "identifier expected" and "illegal start of type".

Is there any thing I can do to make this work?

+1  A: 

You've made a list of arrays. Was that really what you intended, or did you mean to ask about something like

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

?

In any case, familiarize yourself with the Arrays and Collections classes--they contain a lot of utilities that come in handy in cases like this.

Alex Feinman
+1 for finding the generic type conflict
Erick Robertson
Yes. My plan is to build a collection and iterate over it.
Bryan James
@Bryan: right now, your code does not contain 1 collection, it contains many collections--a List of them, in fact, where each element of the List is itself an array. You *might* want to do this, but it's an unusual data structure.
Alex Feinman
@Alex: I do want to do it. How is it unusual? What would you do instead?
Bryan James
It's perfectly valid, but generally I would use a List of Lists or a 2d array.
Alex Feinman
A: 

Easiest way to make a list from an array:

List<String> myList = Arrays.asList("Hello", "There", "Foo", "Bar);

Note the list is fixed size and backed by the input array so changes to the list actually write to the array. Adding or removing elements will likely result in an UnsupportedOperationException. If you intend to mess around with the array such as adding or removing elements you may need to copy the elements into another list

locka
A: 

Check out guava, especially the google collections part.

Several of the collection classes define a static method of(...) which will the add all objects given to that collection.

Combined with static imports this allows very concise colleciton intializations on-the-fly.

e.g.

import static com.google.common.collect.ImmutableList.of;
...

someFunc(of("abc","def","ghi"));

will call someFunc with a list of strings.

Peter Tillemans
A: 

In fact, this code works if you want to make a list of table of integers.

public static void main(String[] args) {
    List<int[]> data = new ArrayList<int[]>();
    data.add(new int[]{21, 19629})
    for(int[] tab : data){//I loop each tab (only one here)
        for(int i: tab){//I loop each values
            System.out.println(i);
        }
    }

}

print 21 19629

oyo
A: 

This may be a limitation of the language syntax, or a bug in the spec that the identifier production is being found before the expression production in the parsing of the method call syntax. Don't mix anonymous array declaration inside the method call.

data.add(new int[]{21, 19629});

and I get "identifier expected" and "illegal start of type".

The "identifier expected" in data.add(...) is probably caused by the compiler expecting an identifier and finding an expression that resolves to an anonymous instance. There are several places where that very syntax is acceptable, I'm not sure why data.add() needs an identifier.

The "illegal start of type" may be that the compiler thinks the expression is an anonymous type declaration but the syntax doesn't allow for that in a method call.

Try creating it using a variable, then pass the variable as the method arg.

int[] nums = new int[] {0,1,2};   
data.add(nums);

In looking for more specifics of the ArrayList.add() grammar, I found this answer on a similar problem someone had learning ArrayList. JavaGlossary has a good set of Compile-time errors.

Kelly French