2014-10-08 43 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函数,以获得您寻找的结果。

相关问题