Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED] Looping through objects in Java
New Posts  All Forums:Forum Nav:

[SOLVED] Looping through objects in Java

post #1 of 8
Thread Starter 
I have a bunch of ArrayList objects named list1, list2, list3...through list9, and I want to iterate through all of them using a for loop.

For example, in VB it would look like this for TextBox controls
Code:
For i = 1 to 9
     Controls("TextBox" & i).Locked = True
Next i

I have tried a similar approach in Java and failed
Code:
ArrayList<Integer> list1 = new ArrayList<Integer>();
     
list1.add(13);

System.out.print(list + "1".get(0)); // fail
System.out.print(("list" + "1").get(0)); // fail
     
String myString = "list1"

System.out.print(myString.get(0)); // fail

any ideas...is this even possible?
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
post #2 of 8
Java doesn't provide a straight forward way to reference a variable or field in scope by its string name, which appears to be what you're trying to do. It does offer a comprehensive reflection API, but I don't think that's what you want.

If you want to iterate over a list of lists, you need something like:
Code:
List<List<Integer>> lists = new ArrayList<List<Integer>>(9);
for (int i = 0; i < 9; i++) {
  lists.add(new ArrayList<Integer>());
}

for(List<Integer> list : lists) {
  // do your thing to each list
}

Or, if you can explain your use case a bit more, there may be a better way.
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #3 of 8
Thread Starter 
Thanks. I'm at work now so I'll look at it tonight and post a reply
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
post #4 of 8
Thread Starter 
Quote:
Originally Posted by jvolkman View Post

Code:
List<List<Integer>> lists = new ArrayList<List<Integer>>(9);
for (int i = 0; i < 9; i++) {
  lists.add(new ArrayList<Integer>());
}

for(List<Integer> list : lists) {
  // do your thing to each list
}

Or, if you can explain your use case a bit more, there may be a better way.

If I'm understanding this correctly one list object is being created and and filled with 9 lists? I don't believe that will work because I need simple direct references to each of the lists that are being created. After the 9 lists are created, how would I access only the 6th list, or maybe just the 1st, 2nd and 3rd?


This is how I'm currently adding variables to my 9 lists
Code:
for (int i = 0; i < 1; i++)
{
     list1.add( i );
     list2.add( i + 1 );
     list3.add( i + 2 );
     list4.add( i + 3 );
     list5.add( i + 4 );
     list6.add( i + 5 );
     list7.add( i + 6 );
     list8.add( i + 7 );
     list9.add( i + 8 );
}

...but I would like to simplify it like
Code:
for (int i = 1; i < 10; i++)
     list(i).add( i );

Edited by semnon - 10/17/13 at 7:02pm
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
post #5 of 8
The List interface allows retrieving an item by index:
Code:
for (int i = 1; i < 10; i++)
     lists.get(i).add( i );

lists.get(7).add(3)

Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #6 of 8
Thread Starter 
Quote:
Originally Posted by jvolkman View Post

The List interface allows retrieving an item by index:
Code:
List<List<Integer>> lists = new ArrayList<List<Integer>>(9);
for (int i = 0; i < 9; i++) {
  lists.add(new ArrayList<Integer>());
}

So the above creates 9 lists, but they are not created literally as list1, list2, list3, etc.?...rather, they are accessed per below?
Code:
for (int i = 1; i < 10; i++)
     lists.get(i).add( i );

lists.get(7).add(3)


So if I wanted to add list3 members to a different list (not in lists), then would I write it as
Code:
coreList.addAll(lists.get(3))
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
post #7 of 8
Yes and yes.
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #8 of 8
Thread Starter 
Quote:
Originally Posted by jvolkman View Post

Yes and yes.

Nice! Thanks for your help.

+rep
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
Realm
(14 items)
 
  
CPUMotherboardRAMHard Drive
i7-4770K ASUS Maximus VI Extreme Patriot Viper Xtreme Intel 730 SSD 
Hard DriveCoolingOSMonitor
Western Digital VelociRaptor Corsair H100i Win 8.1 Acer X223W 
KeyboardPowerCaseMouse
HHKB Professional 2 Corsair AX1200i Danger Den Q20 Razer Mamba 
Mouse Pad
Razer Manticore 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [SOLVED] Looping through objects in Java