-->

Store different types in ArrayList

2020-05-09 23:24发布

问题:

I want to store elements in a list, each elements having 4 parameters I'm trying to create an array list that stores for each element the 4 parameters , which are of different types:

iD: int
x position : float
y position : float
name : string

I use:

ArrayList<String> activList ;

But when I use:

 activList.add(2, 4.5, 8.9,"Name");

I get the error:

" the method add(int,Object) in the type ArrayList is not applicable for the arguments (in, float, float)"

I don't know how to be able to add different types to an ArrayList, is there a way to do it ?

thanks for your help

回答1:

You probably need to create your own class to represent those 4 parameters. Then you can insert instances of that object into the ArrayList:

public class MyParameters {
    private int id;
    private float x;
    private float y;
    private String name;

    public MyParameters(int id, float x, float y, String name) {
        // ...
    }

    // + getters, setters
}

// ...

List<MyParameters> myParameters = new ArrayList<>();
myParameters.add(new MyParameters(2, 4.5, 8.9, "Name"));


回答2:

You could define your ArrayList as being of type <Object> and put whatever in it, but that's not good practice. Create a simple class with your four fields, getters and setters, and use of ArrayList of that type. That'll be much clearer!



回答3:

You can do this like this:

List<Object> test = new ArrayList<>();
test.add(10);
test.add("Test");
test.add(1.0);
test.add('c')

For what you need it'd be good to create a class called for example Storage

class Storage{
   int id;
   String name;
   float x;
   float y;
   public Storage(int id, String name, float x, float y){
     this.id = id;
     this.name = name;
     this.x = x;
     this.y = y;
   }
}

An create List type of Storage



回答4:

Create a class that encapsulates the behavior of the "element"

public class YourClass
{

    public YourClass() { }

    int id;
    float x;
    float y;
    String name;

}

Then you can define a list like so:

ArrayList<YourClass> activList = new ArrayList<YourClass>();

YourClass e = new YourClass();
e.id = 2;
e.x = 4.5;
e.y = 8.9;
e.name = "Name";

activList.add(e);

It would be better to define the fields as private, and access them via getters/setters.



回答5:

Your usage is incorrect here; add doesn't take multiple items to add. What you're looking for instead is this:

activList.add(2);
activList.add(4.5);
activList.add(8.9);
activList.add("Name");

Of course, this still won't work, because your ArrayList holds String objects. If what you want is to just hold string representations of various objects, you can achieve that using the many implementations of toString in Java:

activList.add(Integer.toString(2));
activList.add(Double.toString(4.5));
activList.add(Double.toString(8.9));
activList.add("Name");

But if your goal is to associate names with values, you should really just use a class:

class YourClass {
  private int id;
  private float xPosition;
  private float yPosition;
  private String name;

  // constructor, getters, setters, etc.
}


回答6:

I am not certain whether this is recommended or any shortcomings, but you can initialize the ArrayList without any generics. Then you can add any type of object to the ArrayList

ArrayList al = new ArrayList();
al.add(5);
al.add("Naruto Uzumaki");
al.add(25.24);
System.out.println(al)

This would give you a output of

[5, Naruto Uzumaki, 25.24]