存储在ArrayList的不同类型 [英] Store different types in ArrayList

查看:295
本文介绍了存储在ArrayList的不同类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储在一个列表中的元素,每个元素有4个参数
我试图创建一个存储每个元素的4个参数,这是不同类型的数组列表:

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位置:浮结果
  y位置:浮结果
  名称:字符串

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

我使用的:

ArrayList<String> activList ;

但是当我使用:

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

我得到的错误:

,在ArrayList类型的方法加载(整型,对象)是不适用的参数(在,浮点,浮点型)

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

我不知道如何能够在不同类型添加到一个ArrayList,有没有办法做到这一点?

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

感谢您的帮助。

推荐答案

您可能需要创建自己的类来重新present的4个参数。然后,您可以插入对象的实例到ArrayList中:

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"));

这篇关于存储在ArrayList的不同类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆