-4

Can someone please let me know how to use for loop for ArrayList?

I've code as below

m.value1= new ArrayList<Integer>(Arrays.asList(11,12));
m1.value2 = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5)); 

for loop should take values as (11,1), (11,2),(11,3)... (12,1),(12,2)..

Maroun
  • 94,125
  • 30
  • 188
  • 241
CentosUser
  • 201
  • 1
  • 4
  • 14
  • No, we won't because SO is not here to do your homework. If you can show us what you tried and why and how it is not working, then we can help you solve a *specific* problem. – Dragondraikk Apr 16 '15 at 15:26
  • Asking for a ready solution is not a good way of learning. – Maroun Apr 16 '15 at 15:26
  • possible duplicate of [Ways to iterate over a List in java?](http://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) – FabienAndre Apr 16 '15 at 15:50

1 Answers1

0

Do you mean this?

m.value1= Arrays.asList(11,12);
m1.value2 = Arrays.asList(1,2,3,4,5); 

for(Integer i : m.value1) {
   for(Integer j : m1.value2){
      System.out.println("("+i+","+j+")");
   }
}
Guneli
  • 1,691
  • 1
  • 11
  • 17
  • when I use that it says " cannot convert from element type Object to Integer" – CentosUser Apr 16 '15 at 15:33
  • What is the type of m.value1 or m1.value2 in your code? – Guneli Apr 16 '15 at 15:37
  • It is because Arrays.asList(11,12) returns a List, which then tried to convert to Integer, because of your declaration new ArrayList(Arrays.asList(11,12)); I think the declaration should be just Arrays.asList(11,12));. Try the edited version. – Guneli Apr 16 '15 at 15:39
  • If it does not work, can you, please, add the declaration code of m.value1 and m.value2? – Guneli Apr 16 '15 at 15:40
  • I declared incorrecly. Now it worked :) Thank you. public ArrayList value1 ; public ArrayList value2; --> It worked – CentosUser Apr 16 '15 at 15:54