5

I have an ArrayList, and we'll say it can be at most size 5. I want to assign the first element to var1, the second to var2, the third to var3, etc.

However, sometimes the ArrayList will have less than 5 items, in which case I won't be assigning some variables a value.

So my question is, is there a better way to do this other than:

if (myArrayList.size() > 0)
    var1 = myArrayList.get(0);
if (myArrayList.size() > 1)
    var2 = myArrayList.get(1);
if (myArrayList.size() > 2)
    var3 = myArrayList.get(2);
if (myArrayList.size() > 3)
    var4 = myArrayList.get(3);
if (myArrayList.size() > 4)
    var5 = myArrayList.get(4);
3
  • 2
    why don't you use an array of object itself? First of all you have values stored in the ArrayList, what is that used for? Commented Sep 15, 2011 at 20:17
  • 3
    The only sane way is to avoid it. What do you need all the varX for and why can't you use myArrayList.get(n) instead? Commented Sep 15, 2011 at 20:18
  • No reason, I was just wondering if Java had an easy way to do this to prevent having to type out a few .get(n)'s Commented Sep 15, 2011 at 20:22

5 Answers 5

12

The source of this is most certainly a bad code design. Also to what do you initialize the variable if the arraylist doesn't contain the field (after all you use it later)? A larger example or what exactly you're trying to do would help here. Usually just using

But I can think of at least two ways to do this:

    switch(myArrayList.size()) {
    case 5:
        var4 = myArrayList.get(4);
    case 4:
        var3 = myArrayList.get(3);
    case 3:
        var2 = myArrayList.get(2);
            // and so on
    }

or just use a try/catch.

    try {
        var0 = myArrayList.get(0);
        var1 = myArrayList.get(1);
    }
    catch(IndexOutOfBoundsException ex){
    }

But most certainly it's better to use the arraylist itself and just padd it with the default values you'd otherwise use for your variables.

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

4 Comments

Thanks, I was just curious if Java had some way to do this, guess not though
Not going to downvote, because your first part is useful, but I would never recommend to someone that they use a try/catch to protect them from errors. An exception should be just that: exceptional. You should never run something and expect to get an exception; rather, you're simply prepared for when things go wrong.
@glowcoder I hope you believe me that I'd advocate neither of the two versions ;) But I hope I made it clear enough that one should really overthink the overall approach instead of doing this. About using exceptions in such a way: I do agree that it's not good coding style in Java, but then the switch fall-through is not exactly recommended either. And yes I'm somewhat surprised about 10 upvotes for this, imho I had much more useful posts with only 1-2 - ah well.
@Voo I anticipated that, and your first line says it all "The source of this is most certainly a bad code design", and your last line also indicated they were ~bad ideas~, so that's why I didn't see any reason to downvote. I suppose it could be said that anyone who just skips to the code without reading the surrounding text deserves any trouble they run into that they were warned about in what they skipped.
3

The easiest way to do this is:

Object[] vars = myArrayList.toArray(new Object[5]);

If you insist on having the variables var1 through var5 rather than just using the array elements, copy the array elements to the variables. Alternatively you can replace all instances in your code of "= varN" with "=myArrayList.get(n)".

Comments

2

I agree with Voo that this is most likely from bad code design. But it's an opportunity for me to demonstrate my love of final variables and ternary expressions. :-) How's this? (For yuks I'm presuming an ArrayList of Strings.)

final Iterator<String> iter = myArrayList.iterator();
final String var1 = iter.hasNext() ? iter.next() : null;
final String var2 = iter.hasNext() ? iter.next() : null;
final String var3 = iter.hasNext() ? iter.next() : null;
final String var4 = iter.hasNext() ? iter.next() : null;
final String var5 = iter.hasNext() ? iter.next() : null;

Comments

1

How about using a separate array and assigning the value for each array for corresponding value on the list in a loop?

for (i = 0; i<= 5; i++) {
var [i] = myArrayList.indexOf(i);
}

1 Comment

This is a sensible question, basically asking why they need to be in five separate variables to begin with.
-1

The described design look realy ugly. You can use ternary operator for desired purpose though:

var1 = myArrayList.size() > 0 ? myArrayList.get(0) : "null";

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.