Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an Array of Objects from Arrays in Java

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!

like image 891
Ben Avatar asked Jun 18 '26 19:06

Ben


2 Answers

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.

like image 61
IsaacD Avatar answered Jun 20 '26 07:06

IsaacD


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]));
}
like image 21
W0rmH0le Avatar answered Jun 20 '26 07:06

W0rmH0le



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!