Reputation: 3814
I am new to Android and Java and can't figure out this seemingly basic for loop setup to create an array of objects from the data in two other arrays.
It works this way, but this doesn't feel very sophisticated.
// RefineMenuItem is a public class
public class RefineMenuItem {
private int imageIcon;
private String title;
}
// imageIcon and title are arrays with seven values in them
int[] imageItems = { ... }
String[] menuItems = { ... }
// Create the RefineMenu Objects
RefineMenuItem item1 = new RefineMenuItem(imageItems[0],menuItems[0]);
RefineMenuItem item2 = new RefineMenuItem(imageItems[1],menuItems[1]);
RefineMenuItem item3 = new RefineMenuItem(imageItems[2],menuItems[2]);
RefineMenuItem item4 = new RefineMenuItem(imageItems[3],menuItems[3]);
RefineMenuItem item5 = new RefineMenuItem(imageItems[4],menuItems[4]);
RefineMenuItem item6 = new RefineMenuItem(imageItems[5],menuItems[5]);
RefineMenuItem item7 = new RefineMenuItem(imageItems[6],menuItems[6]);
// Add the RefineMenu Objects to an ArrayList
ArrayList<RefineMenuItem> refineMenuList = new ArrayList<>();
refineMenuList.add(item1);
refineMenuList.add(item2);
refineMenuList.add(item3);
refineMenuList.add(item4);
refineMenuList.add(item5);
refineMenuList.add(item6);
refineMenuList.add(item7);
I believe I need to create the array of objects first and then add to it based on this question.
RefineMenuItem[] arr = new RefineMenuItem[7];
Then I believe I should use a for loop to add to the array, but this is where I'm getting stuck and can't figure it out after researching. Help to point me in the right direction is appreciated!
Upvotes: 2
Views: 482
Reputation: 23
you should be adding it to a class array this way:
import java.util.ArrayList.add
RefineMenuItem[] items = new RefineMenuItem();
for(int i=0 ; i<7 ; i++ ){
RefineMenuItem thing = new RefineMenuItem(RefineMenuItem(imageItems[i],menuItems[i])
items[i].add(i,thing))
}
Upvotes: 0
Reputation: 568
// RefineMenuItem is a public class
public class RefineMenuItem {
private int imageIcon;
private String title;
}
// imageIcon and title are arrays with seven values in them
int[] imageItems = { ... }
String[] menuItems = { ... }
// create array list
ArrayList<RefineMenuItem> refineMenuList = new ArrayList<>();
// add items to the array list in a for loop
for (int i = 0; i < imageItems.length; i++) {
refineMenuList.add(new RefineMenuItem(imageItems[i], menuItems[i]));
}
This is assuming the imageItems
and menuItems
are always the same length
The premise of this is you can create the ArrayList
and then use a for loop to iterate over your imageItems
and menuItems
, create a new RefineMenuItem
from them, and add it to the refineMenuList
Upvotes: 1
Reputation: 54264
A precondition is that imageItems
and menuItems
have the same number of elements in them. So I'd start by asserting that.
Then you can create a new array of the same size, and use a for
loop to iterate:
int[] imageItems = { ... }
String[] menuItems = { ... }
if (imageItems.length != menuItems.length) {
throw new IllegalStateException("array lengths don't match");
}
RefineMenuItem[] result = new RefineMenuItem[imageItems.length];
for (int i = 0; i < imageItems.length; ++i) {
result[i] = new RefineMenuItem(imageItems[i], menuItems[i]);
}
When this code completes, the result
array will be fully populated.
Upvotes: 2
Reputation: 18687
It is impossible to create an ArraysList
directly from two arrays. You first, need to create an array of the RefineMenuItem
objects and add them. However, instead of creating a new array just to add all at once, create them as soon as you instantiate them... Like:
int size;
if(imageItems != null && menuItems != null && menuItems.length == imageItems.length) {
size = menuItems.length;
} else {
size = 0;
}
ArrayList<RefineMenuItem> refineMenuList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
refineMenuList.add(new RefineMenuItem(imageItems[i],menuItems[i]));
}
Upvotes: 2
Reputation: 321
A basic for loop could solve all your problems I believe.
Just make sure you make your implementations/declarations before.
for(int i = 0; i<=6; i++) {
RefineMenuItem item = new RefineMenuItem(imageItems[i],menuItems[i]);
refineMenuList.add(item);
}
I'm gonna be brutally honest, haven't worked with java in a while so there might be some syntax errors but the logical solution should be supplemental.
Upvotes: 5