【问题标题】:Storing objects in arraylist将对象存储在数组列表中
【发布时间】:2012-10-08 05:44:45
【问题描述】:

我在 Java 中使用数组列表时遇到了一个小问题。本质上,我希望将数组存储在数组列表中。我知道数组列表可以保存对象,所以应该是可能的,但我不确定如何。

在大多数情况下,我的数组列表(从文件中解析)只是将一个字符作为字符串保存,但有时它会包含一系列字符,如下所示:

    myarray
0    a
1    a
2    d
3    g
4    d
5    f,s,t
6    r

大多数时候,我唯一关心的位于位置 5 的字符串中的字符是 f,但有时我可能还需要查看 s 或 t。我对此的解决方案是制作一个这样的数组:

      subarray
0     f 
1     s
2     t

并将子数组存储在位置 5 中。

    myarray
0    a
1    a
2    d
3    g
4    d
5    subarray[f,s,t]
6    r

我试图用这段代码来做这个:

 //for the length of the arraylist
 for(int al = 0; al < myarray.size(); al++){
      //check the size of the string
      String value = myarray.get(al);
      int strsz = value.length();
      prse = value.split(dlmcma);
      //if it is bigger than 1 then use a subarray
      if(strsz > 1){
          subarray[0] = prse[0];
          subarray[1] = prse[1];
          subarray[2] = prse[2];
      }
      //set subarray to the location of the string that was too long
      //this is where it all goes horribly wrong
      alt4.set(al, subarray[]);
  }

这不是我想要的方式。它不允许我 .set(int, array)。它只允许 .set(int, string)。有人有建议吗?

【问题讨论】:

  • 什么是alt4(对象声明)?
  • 您确实意识到 String 是一个带有方法的 char[] ,对吧?只需使用具有“a”或“fst”等值的 List,如果您感兴趣的是单个字符,则无需分隔符和拆分。

标签: java arraylist multidimensional-array


【解决方案1】:

也许这就是你想要得到的

public class Tester {

    List<String> myArrays = Arrays.asList(new String[] { "a", "a", "d", "g", "d", "f,s,t", "r" });

    ArrayList<ArrayList<String>> alt4 = new ArrayList<ArrayList<String>>();

    private void manageArray() {
        // for the length of the arraylist
        ArrayList<String> subarray = new ArrayList<String>();
        for(int al = 0; al < myArrays.size(); al++) {
            // check the size of the string
            String value = myArrays.get(al);
            int strsz = value.length();
            String prse[] = value.split(",");
            // if it is bigger than 1 then use a subarray
            if(strsz > 1) {
                for(String string : prse) {
                    subarray.add(string);
                }
            }
            // set subarray to the location of the string that was too long
            // this is where it all goes horribly wrong
            alt4.set(al, subarray);
        }

    }
}

【讨论】:

    【解决方案2】:

    你可以切换到:

    List<List<Character>> alt4 = new ArrayList<List<Character>>();  
    

    【讨论】:

      【解决方案3】:

      我的猜测是您将 alt4 声明为 List&lt;String&gt;,这就是为什么它不允许您将 String 数组设置为列表元素。您应该将其声明为 List&lt;String[]&gt; 并且每个元素都只是单数,只需将其设置为 String[] 数组的第 0 个元素,然后再将其添加到列表中。

      【讨论】:

        【解决方案4】:

        只需将alt4.set(al, subarray[]); 更改为

                 alt4.add(subarray);
        

        我假设alt4 是另一个定义的ArrayList&lt;String[]&gt;。如果不是,定义如下:

                List<String[]> alt4= new ArrayList<String[]>();
        

                ArrayList<String[]> alt4= new ArrayList<String[]>();
        

        【讨论】:

          【解决方案5】:

          最简单的方法是使用 ArrayList 的 ArrayList。

          ArrayList<ArrayList<String>> alt4 = new ArrayList<ArrayList<String>>();
          

          但是,这可能不是最好的解决方案。您可能需要重新考虑您的数据模型并寻找更好的解决方案。

          【讨论】:

          • 是的。我今天会重新考虑。谢谢
          猜你喜欢
          • 2021-08-27
          • 2014-05-23
          • 1970-01-01
          • 1970-01-01
          • 2011-11-11
          • 2023-03-11
          • 1970-01-01
          相关资源
          最近更新 更多