2016-04-27 40 views
0

我想学习如何使用ArrayList的数组。我想我已经很好地理解了阵列,但是我正在为下一个维度而努力。查找阵列的ArrayList索引

你可以从我的下面的代码看到,我只是测试的概念。我有exampleArrayList工作,但我不知道exampleArrayListofArrays。

public void exampleArrayList() { 
     ArrayList<String> al = new ArrayList<String>(); 
     al.add("AZ"); 
     al.add("BY"); 
     al.add("CX"); 
     al.add("DW"); 
     al.add("EV"); 
     al.add("FU"); 
     al.add("GT"); 

     System.out.println("Index of 'AZ': "+al.indexOf("AZ")); 
     System.out.println("Index of 'FU': " + al.indexOf("FU")); 
     System.out.println("Index of 'AA': " + al.indexOf("AA")); 
     System.out.println("Index of 'CX': " + al.indexOf("CX")); 

     for (String row : al){ 
      System.out.println("Value at Index " + al.indexOf(row) + " is " + al.get(al.indexOf(row))); 
     } 
    } 

    public void exampleArrayListofArray() { 
     ArrayList<String []> al = new ArrayList<String []>(); 
     al.add(new String[] {"AB","YZ"}); 
     al.add(new String[] {"CD","WX"}); 
     al.add(new String[] {"EF","UV"}); 
     al.add(new String[] {"GH","ST"}); 
     al.add(new String[] {"IJ","QR"}); 
     al.add(new String[] {"KL","OP"}); 


     System.out.println("Index of 'AB': "+al.indexOf("AB")); 
     System.out.println("Index of 'YZ': "+al.indexOf("YZ")); 
     System.out.println("Index of 'AA': "+al.indexOf("AA")); 
     System.out.println("Index of 'IJ': "+al.indexOf("IJ")); 

     for (String [] row : al){ 
     for (int column = 0; column < 2 ; column ++); 
     System.out.println("Value at Index Row" + row + "Column " 
      + column + " is " + al.get(al.indexOf(row)[column])); 
     } 
    } 

我有两个领域的理解,我需要填写。

1)我怎样才能打印出两个字母的ArrayList索引和相应的数组索引。看来我需要一个循环,但不知道如何继续。我也想知道这些字母何时不存在,比如我假设的“AA”会返回-1的索引。

此外,我想能够打印出Array的ArrayList和由于某种原因,我的println不识别int列。

2)用于exampleArrayList方法我尝试使用这个代码首先指定索引处打印出的值:

System.out.println("Value at Index " + al.indexOf(row) + " is " + al.get(row)); 

而且我得到一个错误:

'get(int)' in 'java.util.ArrayList' cannot be applied to '(java.lang.String)'

而且我不不明白为什么。

感谢您对这些新手问题的帮助。

Airfix膨胀

回答

1
al.indexOf(row)[column] returns a String. 

get方法需要一个int。所以错误是显而易见的。

此外:

 for (String [] row : al){ 
     for (int column = 0; column < 2 ; column ++); 
     System.out.println("Value at Index Row" + row + "Column " 
      + column + " is " + al.get(al.indexOf(row)[column])); // column here is not define!!! 
     } 

试试这个:

  for (String [] row : al) 
      for (int column = 0; column < 2 ; column ++){ 
      System.out.println("Value at Index Row" + row + "Column " 
       + column + " is " + row[column]); 
      } 
0

特定项可以使用row[column]

for (String [] row : al){ 
    for (int column = 0; column < 2 ; column ++); 
    System.out.println("Value at Index Row" + row + "Column " 
     + column + " is " + row[column]); 
    } 
} 

正如你已经得到了数组实例外for循环的指数下进行印刷。现在只需使用列索引即可打印该数组中的值。

System.out.println("Value at Index " + al.indexOf(row) + " is " + al.get(row)); 

是给你的错误在al.get(row)因为ArrayList.get方法接受一个指标是int值。

希望这会有所帮助。

0

数组和列表不适用于反向搜索。可以通过循环遍历它们并测试所有值,但效率极低。相反,尝试一个HashMap。

在这里,您可以存储字符串的索引并稍后再索要。

+0

我不熟悉的HashMap的能力。最终,我希望我的最终代码是这样的:A)从资产文件(约100行,3列)的txt文件填充ArrayList(或HashMap)。 B)基于用户输入文本,我想搜索arrayList列1作为文本,并返回第2列和第3列中的值进行一些计算。 C)然后,如果没有找到用户字符串,我希望用户能够创建第2列和第3列中的值将数据保存到ArrayList(HashMap),按字母顺序对ArrayList进行排序,然后将Array保存回资产文本文件。 – Airfix

+0

您可以使用两个int值定义一个新的RowColumn类,其中包含第2列和第3列的信息。然后,您可以定义HashMap 并将元素放入其中(使用put命令)。通过get,你可以得到每个String的RowColumn。为了读取和写入File linewise,您可以使用apache的FileUtils(或Files类的某些Java 7功能)。 –