Consustore的Java快捷方式

时间:2018-06-18 04:06:12

标签: java syntax

是否有一个已经存在的java类可以替代我用来存储原始数据的这个类?

package scripts.util.function;

import java.io.Serializable;

public class ConsumptionStore implements Serializable {

/**
 * Holds data
 */
private static final long serialVersionUID = -7390032854804146344L;

private Object[] data;

public ConsumptionStore(Object...data){
    this.data = data;
}

@SuppressWarnings("unchecked")
public <T> T get(int index){
    return (T) data[index];
}

public void set(int index, Object value){
    data[index] = value;
}


}

它主要用于存储这样的数据:

OwnedHouses[] houses_I_own;
OwnedVehicles[] vehicles_I_own;
int age;
String name;
new ConsumptionStore(houses_I_own, vehicles_I_own, age, name);

1 个答案:

答案 0 :(得分:0)

Arrays.asList(T... a)与您的构造函数类似,返回的List<T>包含方法get(int index)set(int index, T value)

但是,在列表或数组中存储不同类型的值不是一个好主意。您应该创建一个包含您描述的4个字段的专用类。

class Person {
    private String name;
    private int age;
    private OwnedHouses[] ownedHouses;
    private OwnedVehicles[] ownedVehicles;
    // ...
}