1

I have a class which holds a list inside a list, and its declared as type String, but i no longer know that its a 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

But now the nested list could either be string or integer. How can i fix that? I also can't declare Object must either be string or integer

Also can a single Arraylist hold both ints and strings without declaring as objects?

Edit: It seems you guys are debating the right answer. I will insight why i chose Vikdor answer.

My job is to load a csv, and then user types in command to define each column as string or int. My table class has a private instance variable, and has methods which adds the values. Doing it in Vikdor way makes performing operations on table very easy where i want to merge two table or sort. It will also prevent user from inputting non string or int through an exception.

4

5 回答 5

3

你可能正在寻找这个:

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,并具有与每列关联的适当数据类型。

于 2012-11-03T04:48:42.620 回答
1

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

请按照以下步骤操作

第一步:创建一个类

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

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

谢谢

于 2012-11-03T05:08:12.453 回答
1

您不能将泛型指定为整数或字符串。如果必须同时存储两者,则必须使用共同的父对象,即 Object。

hack 可能是将所有整数转换为字符串。然后,当您从集合中取出对象而不是检查 instanceof 时(就像您使用 Object 时一样),您可以尝试将字符串转换为整数并在对象是细绳。这有点难看,并且如果字符串包含数字,则具有将字符串误解为整数的副作用。

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
于 2012-11-03T05:17:54.003 回答
0

You have to use Object instead of String. Check wethere instance is String or Integer.

Either you can do so if you are wiling to do

 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>());
于 2012-11-03T04:43:08.517 回答
0

你可以使用:

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


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

//其中Data是您使用整数/字符串实现的类

3) private ArrayList<List<String>> table; 

//每当您需要读取转换的值时,字符串也将保存 int 值。

于 2012-11-03T04:49:12.033 回答