2014-10-08 23 views
0

我創建了一個對象Student,使用Comparable與getter/setters以及一個覆蓋compareTo的方法。在單獨的文件中,對象的數組列表由文本文件填充。現在我需要將arraylist中的值與另一個Student對象進行比較。使用Comparable的compareTo來比較一個對象與arraylist中的元素

文件被用來創建一個ArrayList如下:

try { 
    private static ArrayList<Student> array = new ArrayList<Student>(); 
    File file = new File("students.txt"); 
    Scanner scanner = new Scanner(file); 

    while (scanner.hasNextLine()) {     
     String inline = scanner.nextLine(); 
     String[] split = inline.split(":"); 
     Student myStudent = new Student(); 
     myStudent.setUsername(split[0]); 
     myStudent.setPassword(split[1]); 

     array.add(myStudent); 

    } 
    scanner.close(); 
    } 
    catch (FileNotFoundException e) 
    { 
    System.out.println("ERROR."); 
    } 

文本文件看起來像這樣:

約翰:密碼1

簡:密碼2

傑克:password3

(每行一個,中間沒有空行。)

而在一個單獨的方法所創建的學生對象相比,在數組列表中的元素:

Student aStudent = new Student(); 
    aStudent.setUsername("student"); 
    aStudent.setPassword("password"); 
    boolean found = false; 
    for (int i = 0; i < array.size(); i++) 
    { 
    if (array.get(i).compareTo(aStudent) == 0) 
    { 
     System.out.println(aStudent.equals(array.get(i))); 
     found = true; 
     break; 
    } 
    else 
    { 
     System.out.println("No such records found!"); 
     found = false; 
     break; 
    } 
    System.out.println(found); 
    } 

的問題是,該物體aStudent不被用在數組列表中的對象進行比較。它不會爲compareTo調用打印任何東西(一個-1,0或1),但它總是顯示找到的是真的,即使它在文件中沒有與aStudent匹配的情況下應該是false(那裏與用戶名「student」或密碼「password」不匹配)。

所有在一起我的代碼符合和工作 - 它只是工作不正確。

對不起,如果這聽起來很混亂。簡而言之,我的問題是,如何使用Comparable接口和compareTo將arraylist的對象與另一個對象進行比較?如果你能告訴我我做錯了什麼,那麼加號就是了。

預先感謝您。

編輯

這裏是compareTo方法的重載:

public int compareTo(Student obj){ 
    int result = 1; 
    if ((this.Username.compareToIgnoreCase(object.Username) < 0) || (this.Password.compareTo(object.Password) < 0)) 
    { 
     result = -1; 
    } 
    else if ((this.Username.compareToIgnoreCase(object.Username) == 0) && (this.Password.compareTo(object.Password) == 0)) 
    { 
     result = 0; 
    } 

    return result; 

    } 
+0

你在實現Comparable接口並重寫compareTo方法嗎?這裏是有用的鏈接:http://stackoverflow.com/questions/18757805/implementing-custom-compareto – 2014-10-08 03:22:31

+0

@sushanttambare感謝您的回覆和鏈接。是的,我重寫compareTo方法的方式類似於鏈接中顯示的方式。當我創建和比較兩個Student對象(例如,aStudent.compareTo(bStudent))時,它按預期工作,所以我猜想問題在於arraylist,但我無法弄清楚導致問題的原因。 – 2014-10-08 03:26:34

+0

你似乎過早地打破了循環。也就是說,'else'語句打破了循環,這意味着你只會比較數組中的第一個元素,而沒有更多... – MadProgrammer 2014-10-08 03:28:44

回答

1

更多情況下是有用的,但你的for-loop看起來錯...

for (int i = 0; i < array.size(); i++) 
{ 
    if (array.get(i).compareTo(aStudent) == 0) 
    { 
     System.out.println(aStudent.equals(array.get(i))); 
     found = true; 
     break; // out of loop 
    } 
    else 
    { 
     System.out.println("No such records found!"); 
     found = false; 
     break; // break out loop 
    } 
    System.out.println(found); 
} 

break語句用於跳出循環的,這意味着你將永遠只能比較第一個元素列表。

整個else分支不需要(或至少我不認爲這是;)),例如......根據你新

for (int i = 0; i < array.size(); i++) 
{ 
    if (array.get(i).compareTo(aStudent) == 0) 
    { 
     System.out.println(aStudent.equals(array.get(i))); 
     found = true; 
     break; // out of loop 
    } 
} 
System.out.println(found); 

更新

compareTo代碼片段,這...

if ((this.Username.compareToIgnoreCase(object.Username) < 0) || (this.Password.compareTo(object.Password) < 0)) 
{ 
    result = -1; 
} 
else if ((this.Username.compareToIgnoreCase(object.Username) < 0) && (this.Password.compareTo(object.Password) < 0)) 
{ 
    result = 0; 
} 

看來我錯了......在else if應該更像

else if ((this.Username.compareToIgnoreCase(object.Username) == 0) && (this.Password.compareTo(object.Password) == 0)) 

如果爲Comparable接口的合同得到滿足,其中0等於...

例如...

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Test { 

    private static ArrayList<Student> array = new ArrayList<Student>(); 

    public static void main(String[] args) { 
     array.add(new Student("John", "password1")); 
     array.add(new Student("Jane", "password2")); 
     array.add(new Student("Jack", "password3")); 

     Student aStudent = new Student("Jack", "password3"); 
     boolean found = false; 
     for (int i = 0; i < array.size(); i++) { 
      if (array.get(i).compareTo(aStudent) == 0) { 
       System.out.println(aStudent.equals(array.get(i))); 
       found = true; 
       break; 
      } 
     } 
     System.out.println(found); 
    } 

    public static class Student implements Comparable<Student> { 

     private String name; 
     private String password; 

     public Student(String name, String password) { 
      this.name = name; 
      this.password = password; 
     } 

     public String getName() { 
      return name; 
     } 

     public String getPassword() { 
      return password; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public void setPassword(String password) { 
      this.password = password; 
     } 

     @Override 
     public int compareTo(Student object) { 
      int result = 1; 
      if ((this.getName().compareToIgnoreCase(object.getName()) < 0) || (this.getPassword().compareTo(object.getPassword()) < 0)) { 
       result = -1; 
      } else if ((this.getName().compareToIgnoreCase(object.getName()) == 0) && (this.getPassword().compareTo(object.getPassword()) == 0)) { 
       result = 0; 
      } 

      return result; 

     } 
    } 

} 

這將打印出...

false 
true 

如果對象不是equal但是其中e他們是可比較的......這對我來說有點奇怪......)

+0

非常感謝。這對我來說也很奇怪,但它很有用!非常感謝您的幫助! – 2014-10-08 03:53:49

+0

以這種方式使用「Comparable」是一個「小」奇怪的事情(恕我直言),在那裏你可以重寫equals並獲得相同的功能...... – MadProgrammer 2014-10-08 03:59:50

0

您的問題可能在於的compareTo功能,你推翻,你需要包括代碼,否則沒有人能確定爲什麼某些所返回的值

編輯:

注意創建對象時,他們不一定等於完全掌控因爲它們所包含的值是相等的。它們是對象的單獨實例並被視爲這樣。

您需要重寫equals函數,而不僅僅是compareTo函數,以獲得您尋找的結果。

相關問題