-1

I have the same problem like this post Create an arraylist with multiple object types?

I have a list of objects: List<Object[]> and I would have List<String,Float>

and I want do like the reply of GV that I make a custom model class with two parameters one Integer and other String. Then using an ArrayList of that object.

but I don't know how can I return this List with a multiple types.

2
  • I would recommend having separate lists, one List<YourCustomModelClass> and another List<WhateverYouMayStoreHere>. Commented Aug 27, 2014 at 21:40
  • Would a Map<String,Float> work? Otherwise a List<YourCustomWrapper> Commented Aug 27, 2014 at 21:41

3 Answers 3

1

You don't want to create a List<String,Float> (not to mention it's impossible). Instead, as you allude, you want to create a custom class that holds a String and an Integer, and create a list of that.

For instance:

public class Model {
  private final String name;
  private final int count;

  public Model(String n, int count) { ... }
  public String getName() { ... }
  public int getCount() { ... }
}

Now you can create List<Model> instances containing the data you need.

List<Model> ls = new ArrayList<>();
ls.add(new Model("Jim", 10));
Sign up to request clarification or add additional context in comments.

Comments

0

If you create an object with properties of type String and Integer then you can use the List of that object type.

class YourObject{
   String s;
   Integer i;
   // Contructor(s) and stuff...
}

and then:

List<YourObject> list = new ArrayList<YourObject>();

O hope that helps.

Comments

0

or if you really really want to have a list of objects, when you get th object from the list you can check its type using instanceof

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.