0

Programming with Java, I have an ArrayList<Integer> with values of [1,2,3] and I want to create separate integer variables a, b, and c that contain values from the ArrayList in the order that I create them. In Python I would:

l = [1,2,3]
a,b,c = l

and a, b, and c would have the values 1, 2, 3, respectively. How would you do that in Java?

2
  • Auugh, JB Nizet's and AlexWien's take too much lines... is there some way I can do it with mapping or something? Commented Feb 5, 2014 at 19:38
  • No. That's the only way. That's something or very rarely need to do anyway. Most of the time, if you have a list, you're not interested in its first three elements, but on all of them, so you iterate on them. My guess is that you're using an array to store three values that should be attributes of an object instead. But you aren't telling us anything about what your actual usecase is. Commented Feb 5, 2014 at 22:23

2 Answers 2

1
int i = 0;
int a = list.get(i++);
int b = list.get(i++);
int c = list.get(i++);
Sign up to request clarification or add additional context in comments.

Comments

1
Integer a = list.get(0);
Integer b = list.get(1);
Integer c = list.get(2);

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.