Samet
Samet

Reputation: 917

How to manipulate ArrayList of Object[]?

Suppose I have an ArrayList containing Object[]{"Hello",10,abitmap} at index 0 and Object[]{"How are you?",20,bbitmap} at index 1

How can I get for example "Hello" from index 0? How can I replace 20 by 15in index 1?

How can I fill all the third columns in the ArrayList by cbitmap using Collections.fill for example?

Thanks for your help.

Upvotes: 0

Views: 6581

Answers (4)

DejanLekic
DejanLekic

Reputation: 19797

How can I get for example "Hello" from index 0?

Object hello = myArray.get(0)[0];

How can I replace 20 by 15 in index 1?

myArray.get(1)[1] = new Integer(15);

Upvotes: 2

rtheunissen
rtheunissen

Reputation: 7435

Instead of having an Object [], create an inner class, such as:

private class ImageObject{
     private String name;
     private int size; 
     private BufferedImage bitmap;

  public ImageObject(String name, int size, BufferedImage bitmap){
     this.name = name;
     this.size = size;
     this.bitmap = bitmap;
  }

  public String getName(){ 
     return name; 
  }

  public int getSize(){ 
     return size; 
  }

  public BufferedImage getBitmap(){ 
     return bitmap; 
  }

  public void setName(String name){ 
     this.name = name; 
  }

  public void setSize(int size){ 
     this.size = size; 
  }

  public void setBitmap(BufferedImage bitmap){ 
     this.bitmap = bitmap; 
  }
}

Then, create your ArrayList like this:
ArrayList<ImageObject> objects = new ArrayList<ImageObject>();

Upvotes: 3

Andy Petrella
Andy Petrella

Reputation: 4345

Sorry if I'm rough (or wrong) but it seems that you've not modelize in the right way. Even if you can easily do what you need to do, I would recommend you to think things in another way.

That is, an OO way.

Instead of storing heterogenous things in an generic array, let's create a class that hold your three information with the according structure and semantic.

class MyStuff {
   private String name;
   private int anInt;
   private List bitmap; //WARN ::  here I guess that it would be preferable to have something else like an Image object, or a stream, or ...

   MyStuff() {}

   //GETTERS AND SETTERS
}

Now, update properties is trivial, and retrieve them too actually.

And to have all of 'em in a List, you will have the convenience to use Generics

List<MyStuff> myStuffs = new ArrayList();
myStuffs.add(...);
myStuffs.add(...);

myStuffs.get(0).setAnInt(4)
myStuffs.get(0).setName("newName")

Upvotes: 3

Howard
Howard

Reputation: 39197

Answers to your questions put into code:

ArrayList<Object[]> list = new ArrayList<Object[]>();
list.add(new Object[]{"Hello",10,abitmap});
list.add(new Object[]{"How are you?",20,bbitmap});

Object hello = list.get(0)[0];   // get the first item of the first list entry
System.out.println(hello);

list.get(1)[1] = 15;             // set the second item of the second list entry
System.out.println(list.get(1)[1]);

Please consider also to build a custom class as others suggested in the comments.

Upvotes: 1

Related Questions