2017-02-21 11 views
-1

私は「返信」が存在するかどうか確認したいと思います。私のプログラムでは、私のプログラムを実行すると、うまくいかず、ちょっと失礼です。返信が私のarraylistに存在するなら、私は最後の要素とその要素を交換したい。私も.matchesクラスを使ってみましたが、うまくいきませんでした。arraylist内の文字列を確認するにはどうすればよいですか?

case 2: 
     System.out.println("What employee do you want to remove, Enter full name: "); 

     String reply = input.next(); 

     for(String checkName : EmployeeNameList) 

      if(checkName.equalsIgnoreCase(reply)){ 

       swapByIndex(EmployeeSalaryList, swapList(EmployeeNameList,reply)); 

      } 

      break; 

これは私のスワップ方式です。

public static<T>int swapList(ArrayList<T> list, T reference){ 
    T t = null; 
    T i; 

    for(T ref : list){ 
     if(ref.equals(reference)){ 
      t = ref; 
     } 

    } 

    i= list.get(list.size()-1); 
    list.set(list.indexOf(t),i); 
    list.set(list.size()-1,t); 

    return list.indexOf(t); 
} 
public static<T> void swapByIndex(ArrayList<T> list, int index){ 
    T t; 
    t = list.get(list.size()-1); 
    list.set(index, t); 
    list.set(list.indexOf(t), list.get(index)); 


} 
+1

はそれが役立つかもしれませんあなたに何が起こるか教えてください。ちょうどあなたが1つを得ると言うのではなく。 – Kayaman

+0

ええ、私の悪い、現時点では、私はエラーが発生しない、それはちょうど動作しません – Jesper

+0

"動作しない"を定義する - それは今何をし、代わりに何を期待したのですか? – EJoshuaS

答えて

0

if(EmployeeNameList.contains(reply)){ 
       EmployeeNameList.set(EmployeeNameList.indexOf(reply), list.get(list.size()-1)); 
       EmployeeNameList.set(EmployeeNameList.size()-1, reply); 
    } 
0

のArrayListのメソッドcontaisを使用し

例:実際にあれば

あなたはリストにすでに定義されたメソッドを使用してそれを行うことができます
public void containsExample(){ 
    ArrayList<String> array = new ArrayList<>(); 
    array.add("example1"); 
    array.add("example2"); 
    array.add("example3"); 
    if(array.contains("example1")){ 
     System.out.println("example1 exists"); 
    } 
} 
関連する問題