2013-07-04 42 views
0

我需要比较列表中的值和数组中的值。 我写道:比较列表和数组

public class JavaApplication3 { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    // TODO code application logic hereut 
    List<String> l = new ArrayList<String>(); 
    l.add("test"); 
    l.add("b"); 
    String v = ""; 
    String s = ""; 
    String[] arr = {"test", "c", "b"}; 

    for (int i = 0; i < l.size(); i++){ 
     v = ""; 
     s = ""; 
     //System.out.println(l.get(i)); 
     for (int j = 0; j < arr.length; j++){ 
      if (l.get(i).equals(arr[j])){ 
       s = i + ""; 
      }else{ 
       s = arr[i]; 
      } 
      v = v + s + ",";     
     } 
     System.out.println(v);   
    } 

} 

}

我获得以下 0,测试,测试, C,C,1 但我需要这样的结果: 0,C,1 ,

回答

0

如何:

public static void main(String[] args) { 
     // TODO code application logic hereut 
     List<String> l = new ArrayList<String>(); 
     l.add("test"); 
     l.add("b"); 
     String v = ""; 
     String s = ""; 
     String[] arr = {"test", "c", "b"}; 
     int pointer = 0; 
     for (int i = 0; i < l.size(); i++){ 
         //System.out.println(l.get(i)); 
      for (; pointer < arr.length;){ 
       if (l.get(i).equals(arr[pointer])){ 
        s = i + ""; 
        v = v + s + ","; 
        pointer++; 
        break; 
       }else{ 
        s = arr[i]; 
       } 
       pointer++; 
       v = v + s + ","; 
      } 

     } 
     System.out.println(v); 

    } 
+0

嗨,我得到线(指针错误 coco

+0

预计 不是一个声明 ';'预期 不兼容的类型 要求:发现布尔 :整数 – coco

+0

如果'什么(;指针 Elbek

1

看着你我预期的结果客串s的要求是这样的:

对于数组中的每个元素,检查它是否在列表中。如果它在列表中,则从列表中打印此元素的索引,否则打印元素本身。所以算法应该这样做:

array[0] = "test" -> found at index 0 -> print "0" 
array[1] = "c" -> not found  -> print "c" 
array[2] = "b" -> found at index 1 -> print "1" 

外部循环应迭代数组。然后,对于每个数组项,迭代列表直到找到相同的元素。对于第一稿,不要在字符串中收集输出,而是立即打印输出。当算法按预期工作时,您可以创建字符串。

+0

好吧,我会尝试 – coco

+0

很多谢谢安德烈亚斯和你们所有人。好主意 :)。愚蠢的问题作为初学者。谢谢 – coco

0

您有六次迭代,每次迭代都会在输出中插入一些东西。

您需要三次迭代,每次迭代检查第一个列表中的成员身份。你可以用List.contains()方法来做到这一点。 (如果列表很长,则可能需要考虑使用Set而不是List,以便更快地检查设置成员资格。)

0

尝试将事情分解到其高级步骤。

For each string in the array 
    find its place in the list 
    if the item is in the list 
     print its position 
    else 
     print the missing string 
    print a common and space 

一旦你有了这个,你可以发现这find its place in the list可能是在列表中返回的地方或-1,如果它不在列表的方法。这就是我所做的(可能已经重命名了一些东西,并使用了一个StringBuilder,但你暂时可以忽略它)。

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

public class Example { 
    public static void main(final String[] args) { 
     final List<String> listToSeach = new ArrayList<String>(); 
     listToSeach.add("test"); 
     listToSeach.add("b"); 

     final String[] arrayElementsToFind = { "test", "c", "b" }; 

     final StringBuilder output = new StringBuilder(); 
     for (final String string : arrayElementsToFind) { 
      final int firstIndex = findFirstIndex(listToSeach, string); 
      if (firstIndex > -1) { 
       output.append(firstIndex); 
      } else { 
       output.append(string); 
      } 
      output.append(", "); 
     } 
     System.out.println(output); 
    } 

    private static int findFirstIndex(final List<String> list, 
      final String element) { 
     for (int i = 0; i < list.size(); i++) { 
      if (list.get(i).equals(element)) { 
       return i; 
      } 
     } 
     return -1; 
    } 
} 
0

那么我的建议是:

 List<String> l = new ArrayList<String>(); 
    l.add("test"); 
    l.add("b"); 
    String[] arr = {"test", "c", "b"}; 

    for(int i=0;i<arr.length;++i){ 

     if(l.contains(arr[i])) 
     s = ""+l.indexOf(arr[i]); 
     else 
     s = arr[i]; 

     v = v + s + ","; 
     } 

如果得到了你说是正确的,我觉得这是更简洁