在Java中实现混合类型列表。

huangapple go评论79阅读模式
英文:

Implementing a mixed-type list in Java

问题

我正在尝试编写一个混合类型的列表,以便在同一个列表中既可以容纳整数又可以容纳字符串。
我有一个customType类,可以容纳一个整数和一个字符串。
当我向列表中添加项时,add函数会将元素放入相应的变量中。


public class customType {
    private String str="";
    private int integer=0;
    private String type="";
    private Object NumberFormatException;

    public customType(int integer){
        this.integer=integer;
        type="INTEGER";
    }

    public customType(String str){
        this.str=str;
        type="STRING";
    }
}

由于add函数的参数类型不同,我可以进行重载。

public class customTypeList {

private ArrayList<customType> elements = new ArrayList<customType>();

.
.
.

public void add(String str){
    elements.add(new customType(str));
    }

public void add(Integer integer){
    elements.add(new customType(integer));
    }
}

但是我无法定义一个get函数,因为唯一的返回类型不同,并且Java不允许仅对返回类型不同的函数进行重载。


public int get(int i){
    return elements.get(i).getInteger();
    }

public String get(int i){
    return elements.get(i).getStr();
    }

我知道,我可以使用两个ArrayList来实现这一点,但我想要像这样实现混合类型的列表。
有方法可以做到这点吗?

英文:

I am trying to write a mixed-type list so it can hold integers and strings on the same list.
I have a customType class that can hold one integer and one string.
When I add items to my list, the add function puts elements to the appropriate variable.


public class customType {
    private String str=&quot;&quot;;
    private int integer=0;
    private String type=&quot;&quot;;
    private Object NumberFormatException;

    public customType(int integer){
        this.integer=integer;
        type=&quot;INTEGER&quot;;
    }

    public customType(String str){
        this.str=str;
        type=&quot;STRING&quot;;
    }
}

Since the parameter type of add function is different, I can overload it.

public class customTypeList {

private ArrayList&lt;customType&gt; elements = new ArrayList&lt;customType&gt;();

.
.
.

public void add(String str){
    elements.add(new customType(str));
    }

public void add(Integer integer){
    elements.add(new customType(integer));
    }
}

But I can not define a get function because the only return type is different and Java is not allowing overload for only different return type functions.


public int get(int i){
    return elements.get(i).getInteger();
    }

public String get(int i){
    return elements.get(i).getStr();
    }

I know, I can use two ArrayLists for this, but I want to implement a mixed-type list like this.
Is there a way to do that?

答案1

得分: 2

你可以举例来创建一个“原始”列表,这是一个可以接受类型为 Object 的对象的列表。

new ArrayList<Object>();

然后,当你想要特殊处理整数或字符串时,你需要使用 instanceof 运算符来检查对象是否为整数或字符串,并相应地进行类型转换。

if (object instanceof Integer) {
  Integer i = (Integer) object;
}

第二种方法是为每种类型的对象创建不同的方法。例如:

public int getInteger(int i);
public String getString(int i);
英文:

You could for example create a "raw" list, that is a list that accepts objects of type Object

new ArrayList&lt;Object&gt;();

Then, when you want special handling of Integers or Strings, you'd have to use the instanceof operator to check if the object is an Integer or a String and cast it accordingly.

if(object instanceof Integer) {
  Integer i = (Integer) object;
}

A second approach would be to create different methods for each type of object. I.e.:

public int getInteger(int i);
public String getString(int i);

huangapple
  • 本文由 发表于 2020年10月14日 04:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64342599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定