2015-06-29 41 views
-1

我有这种方法。我想从一个包含0列表或空值,其中在列表中的数值如下如何从通用列表中删除特定行

rollno name age city street zipcode 
1  abc 0 pqr xyz 145202 

年龄具有价值为零删除行,我们必须将其删除。任何机构都可以帮我用它我是新的Java?

在下面的代码,我删除行,我再次打印列表

public void validateData(List<Student> studentList) throws InsufficientDataException { 
    System.out.println(String.valueOf(list)); 
    for (Iterator <Student> iter = list.listIterator(); iter.hasNext();) { 
     Student a = iter.next(); 
     if (list.contains("null")) { 
      iter.remove(); 
     } 
    } 
    System.out.println(list); 
} 
+0

什么是'Student'代码读取的Javadoc?它是属性'rollno','name','age','city','street'和'zipcode'的bean吗? – tilois

+0

已经在这里提出http://stackoverflow.com/questions/31112429/how-to-check-whether-data-in-the-list-contains-0-or-null-value-and-remove-that-d/ 31112972#31112972 –

回答

0

假设学生类的领域是公开的,你的列表可以包含空值。

public void validateData (List<Student> studentList) throws InsufficientDataException { 
     for (Iterator<Student> iter = list.listIterator(); iter.hasNext();) { 
      Student a = iter.next(); 
      if (a == null) { // Check if a is null 
       iter.remove(); // remove it because it is null 

      } else {// a is not null 
       if (a.age == 0) { // check if age is 0 
        iter.remove(); //Remove it because age is 0 
       } 
      } 
     } 
    } 

从迭代器获得的变量'a'是您在每次迭代时获得的列表元素。您需要检查变量是否为空,或者是否包含年龄为0的学生,而不是检查列表是否包含空值。

if (list.contains("null")) { //This is wrong. 
     iter.remove(); 
} 

最后,我会强烈建议您在列表

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

相关问题