未知的数据类型声明

时间:2012-11-03 04:41:48

标签: java string integer declaration

我有一个类,它在列表中包含一个列表,并将其声明为String类型,但我不再知道它是一个字符串。

private ArrayList<List<String>> table;
table = new ArrayList<List<String>>();
table.add(new ArrayList<String>()); //is possible
table.add(new ArrayList<Integer>()); //not possible

但现在嵌套列表可以是字符串也可以是整数。我该如何解决这个问题?我也不能声明Object必须是字符串或整数

单个Arraylist也可以同时保存整数和字符串而不声明为对象吗?

编辑:看来你们正在辩论正确的答案。我会洞察为什么我选择了Vikdor的答案。

我的工作是加载csv,然后用户输入命令将每列定义为string或int。我的表类有一个私有实例变量,并且有添加值的方法。以Vikdor方式执行此操作使得在表上执行操作非常容易,我想合并两个表或排序。它还会阻止用户通过异常输入非字符串或int。

5 个答案:

答案 0 :(得分:2)

您可能正在寻找:

List<List<? extends Object>> table;
table = new ArrayList<List<? extends Object>>();
table.add(new ArrayList<String>()); 
table.add(new ArrayList<Integer>()); 

当您检索内部列表时,这将导致未经检查的代码,如下所示(显然不建议在生产代码中使用):

List<String> stringList = (List<String>) table.get(0); // Unchecked cast.

来自OP的评论:

  

因为我要求将它作为int或string。否则,您可以在列表中添加超过这些类型。嵌套列表以列的形式保存表的值,每列包含int或string。你有任何其他动态的方式来读取csv文件并存储在表中吗?

最好使用适当的CSV处理器(如SuperCSV),该处理器将直接解析为具有与每列相关的适当数据类型的POJO。

答案 1 :(得分:1)

单个Arraylist可以同时保存int和字符串

按照以下步骤

第1步:创建一个类

import java.io.*;
public class TempClassArray 
{
    int numValue;
    String strValue;
    public TempClassArray(int numValue,String strValue)
     {
        this.numValue=numValue;
        this.strValue=strValue;
     }
}

步骤2:在同一个目录中创建另一个用于在同一个数组列表中存储整数和字符串的类

import java.util.*;
public class mainClass 
{
    public static void main(String args[])
    {
        List<TempClassArray> sample=new ArrayList<TempClassArray>();
        sample.add(new TempClassArray(1,"One"));  //adding both integer and string values
        sample.add(new TempClassArray(2,"Two"));
        sample.add(new TempClassArray(3,"Three"));
        sample.add(new TempClassArray(4,"Four"));
        sample.add(new TempClassArray(5,"Five"));
        sample.add(new TempClassArray(6,"Six"));

        System.out.println("Integer \t\t String");
        System.out.println("********\t\t********");
        for(TempClassArray s:sample)
        {
            System.out.println(s.numValue+"\t\t\t"+s.strValue);
        }
    }
}

<强>输出

Integer          String
********        ********
1           One
2           Two
3           Three
4           Four
5           Five
6           Six

像这样,您可以在单个数组列表中添加许多数据类型和值

谢谢

答案 2 :(得分:1)

您不能将泛型指定为Integer或String。如果必须存储两者,则必须使用公共父对象,即Object。

hack可能是将所有整数转换为字符串。然后,当你从集合中获取对象而不是检查instanceof时(就像你使用Object时那样),你可以尝试将字符串转换为整数并在对象是一个时捕获数字格式异常串。这有点难看,并且如果字符串包含数字,则会产生将String误解为整数的副作用。

package testapp;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author joefitz
 */
public class IntsAndStrings
{
    List<String> intsAndStrings = new ArrayList<String>();

    public void put(String val)
    {
        intsAndStrings.add(val);
    }

    public void put(Integer val)
    {
        intsAndStrings.add(String.valueOf(val));
    }

    public String getString(int index)
    {
        return intsAndStrings.get(index);
    }

    public int getInt(int index)
    {
        return Integer.parseInt(intsAndStrings.get(index));
    }

    public boolean isInteger(int index)
    {
        boolean retVal = false;

        try
        {
            Integer.parseInt(intsAndStrings.get(index));
            retVal = true;
        }
        catch(NumberFormatException e)
        {
        }
        return retVal;
    }    


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        IntsAndStrings testApp = new IntsAndStrings();
        testApp.put(1);
        testApp.put(2);
        testApp.put("hello");
        testApp.put("world");

        for (int i = 0; i < testApp.intsAndStrings.size(); i++)
        {
            if (testApp.isInteger(i))
            {
                int intVal = testApp.getInt(i);
                System.out.println("int:" + intVal);
            }
            else
            {
                System.out.println("String: " + testApp.getString(i));
            }
        }
    }    
}

运行结果:

int:1
int:2
String: hello
String: world

答案 3 :(得分:0)

您必须使用Object而不是String。检查实例是String还是Integer。

如果你愿意,你可以这样做

 ArrayList<List<String>> tableStr;
     ArrayList<List<Integer>> tableInt;



    tableStr = new ArrayList<List<String>>();
    tableInt = new ArrayList<List<Integer>>();

    tableStr.add(new ArrayList<String>()); 
    tableInt.add(new ArrayList<Integer>());

答案 4 :(得分:0)

您可以使用:

1) private ArrayList<List<Object>> table;


2) private ArrayList<List<Data>> table; 

//其中Data是一个用整数/字符串

实现的类
3) private ArrayList<List<String>> table; 

//当你需要读取你转换的值时,String也会保存int值。