0

How would I shorten this?

int[] a1 = {2, 0, 1, 4, 3};
int[] a2 = {3, 1, 2, 0, 4};
int[] a3 = {4, 2, 3, 1, 0}; 
int[] a4 = {0, 3, 4, 2, 1};
int[] a5 = {1, 4, 0, 3, 2};
ArrayList<int[]> array = new ArrayList<int[]>();
array.add(a1);
array.add(a2);
array.add(a3);
array.add(a4);
array.add(a5);
3
  • Don't use an array list? just use int[][]? Commented Jan 19, 2010 at 1:14
  • 5
    Short code != Efficient code. Commented Jan 19, 2010 at 1:16
  • 1
    Looks like a problem where I'd go straight for a 'matrix' class with a single array (more so I don't have to manage all the nested array weirdness rather than the premature optimisation). Commented Jan 19, 2010 at 1:43

4 Answers 4

3
List<int[]> ints = Arrays.asList(new int[][]{{2, 0, 1, 4, 3},
                                             {3, 1, 2, 0, 4},
                                             {4, 2, 3, 1, 0},
                                             {0, 3, 4, 2, 1},
                                             {1, 4, 0, 3, 2}});

is one way.

Edit: bmargulies rightly pointed out that the resulting List is not necessarily an ArrayList; if one is needed, you can then copy the elements into a new one.

Sign up to request clarification or add additional context in comments.

6 Comments

This is Java 1.4, yes? In Java 5+ you'd just list the elements: Arrays.asList({1,2,3},{4,5,6}); The resulting list is fixed-length.
@bmargulies: not necessarily, but the conversion is simple enough. I'll add that to the answer.
@Michael Brewer-Davis: I'd think that would work, but for some reason it gives a compile-time error.
Seems like it's because there's nothing to cause creation of an array object.
Arrays.asList does return an ArrayList, but it is a java.util.Arrays.ArrayList not a java.util.ArrayList (nice naming). :)
|
2

K.I.S.S.:

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

It does the same thing as the old code, is shorter, and performs fine.

Comments

0

With Arrays.asList

    int[][] a = {
            {2, 0, 1, 4, 3},
            {3, 1, 2, 0, 4},
            {4, 2, 3, 1, 0}, 
            {0, 3, 4, 2, 1},
            {1, 4, 0, 3, 2}
    };
    List<int[]> arr = Arrays.asList(a);

Comments

0

Do you just want 'array' to contain the same information? Something like:

int[][] a = {{2, 0, 1, 4, 3}, {3, 1, 2, 0, 4}, {3, 1, 2, 0, 4}, {0, 3, 4, 2, 1}, {1, 4, 0, 3, 2}};
    ArrayList<int[]> array = new ArrayList<int[]>();
    for(int[] i: a)
        array.add(i);

is shorter than what you posted. Are you trying to shorten the declaration of the variables a1-5, or the repetitive calls to add, or both?

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.