2014-10-29 48 views
3

我们如何一起使用Arrays和ArrayLists(如果可能的话)?ArrayLists和Arrays如何在Java中相互交互?

我想储存一些字符串到一个ArrayList,后来拉出来,它们解析成字符串数组,这些数组存储在一个ArrayList,并能够检索以后的ArrayList中的数据...

看看这段代码,随时带我分开做蹩脚的编码;自从我使用Java以来​​已经有一段时间了。

public static void parseData() throws IOException 
{ 
    //for reference, nonParsedData is ArrayList<String> 
    // while parsedData is ArrayList<String[]> 
    String line = new String(); 
    String[] tempContainer = new String[7]; 
    Scanner keyboard = new Scanner(System.in); 

    for (int x = 0; x < nonParsedData.size(); x++) 
    { 
     line = nonParsedData.get(x); 

     //filling the temporary container to place into the ArrayList of arrays 
     tempContainer[0] = line.substring(0,1); //Data piece 1 
     tempContainer[1] = line.substring(1,3); //Data piece 2 
     tempContainer[2] = line.substring(3,7); //Data piece 3 
     tempContainer[3] = line.substring(7,8); //Data piece 4 
     tempContainer[4] = line.substring(8,9); //Data piece 5 
     tempContainer[5] = line.substring(9,10); //Data piece 6 
     tempContainer[6] = line.substring(10,(line.length() - 1)); //Data piece 7 



     parsedData.add(tempContainer); 
    } 
} 

所以在前面,我已经倾倒了一些外部文件到nonParsedData。这是一串字符串。没什么大不了。我把这些字符串,读取它们,把它们放入数组中。 Hunky dory。这工作正常。

这是检索过程,这使我中风。三次 -

public static void fetchAndPrint() throws IOException 
{ 
    String[] tempContainer = new String[7]; 

    for(int x = 0; x < parsedData.size(); x++) 
    { 

     tempContainer = parsedData.get(x); //this should be assigning my stored array to 
              //the tempContainer array (I think) 

     //let's try deepToString (that didn't work) 
     //System.out.println((parsedData.get(x)).toString()); //just a debugging check 

     System.out.println(x); 
     System.out.println(tempContainer[0] + " " + tempContainer[1] + " " 
      + tempContainer[2] + " " + tempContainer[3] + " " 
      + tempContainer[4] + " " + tempContainer[5] + " " 
      + tempContainer[6] + " "); 
    } 
} 

我这样做之后,只有我一个输出从最初ArrayList<String[]>显示出来!我在循环中错过了什么吗?当我真的没有这个功能时,我是否正在实施ArrayList的阵列?我不正确地给数组赋值? ArrayList甚至会给我一个实际的数组 - 或者它只是一个像{1,2,3等}的列表?这些事情甚至不同吗?

最后,我知道这可以使用数据库语言来实现,但我们假设我想坚持使用Java。

+0

http://stackoverflow.com/questions/9929321/converting-arraylist-to-array-in-java – EMM 2014-10-29 15:58:32

+0

上述帖子不是我所需要的,但谢谢。使用字符串从ArrayList中创建数组没有问题,它是一旦存储的数组将检索到有问题的数组。 – 2014-10-29 16:43:57

回答

0

由于tempContainer范围的,您覆盖你的价值观:

String[] tempContainer = new String[7]; 
for (int x = 0; x < nonParsedData.size(); x++) 
{ 
    line = nonParsedData.get(x); 

    //filling the temporary container to place into the ArrayList of arrays 
    tempContainer[0] = line.substring(0,1); 
    // ... 
    parsedData.add(tempContainer); // <=== this is always the same array 
} 

你可能想要的是每次迭代这样一个全新的数组:

for (int x = 0; x < nonParsedData.size(); x++) 
{ 
    String[] tempContainer = new String[7]; 
    // fill tempContainer 
    parsedData.add(tempContainer); 

working demo


作为一个方面,你可能想在这里使用一个对象:

  • 定义零件对象(可以称之为Part
  • 为每个循环迭代创建一个新的对象
  • 使用ArrayList<Part>parsedData
+0

所以基本上'对象部分'是一个包含'String []'对象的对象? – 2014-10-29 16:48:53

+1

或7'String'字段或'List '或别的东西,这是一个品味/偏好的问题 – 2014-10-29 16:54:15

+0

为响应'String [] tempContainer'声明的移动,这并不重要;输出仍然...怪异。感谢您的其他建议。会尝试。 – 2014-10-29 17:00:11

1

我就继续它很简单,并回答你的问题:

数组列表和数组如何交互在Java另一个?

首先我们必须区分两者。

虽然阵列(即,tempContainer [])只是连续内存中相似类型数据(基元或对象)的集合,ArrayLists是具有字段和成员函数的对象。

回到你的问题。为了从一个阵列转换为的ArrayList

String[] names = {"Bobby", "Earl", "Super Mario"}; 
ArrayList<String> namesList = new ArrayList<String>(); 

// First we must return the array as a List using Arrays.asList() 
// Then use the addAll() method provided by the ArrayList class. 
namesList.addAll(Arrays.asList(names)); 

现在对于相反,返回一个ArrayList内容阵列

String[] names; 
ArrayList<String> namesList = new ArrayList<String>(); 

//All that is needed is the ArrayList member method toArray(). 
names = namesList.toArray(); 

我希望这有助于!

+0

谢谢拉夫;它更多的是当数组放置在ArrayLists中时它们如何操作。这是很好的信息,并表示赞赏。 – 2014-10-29 23:07:31