用arolylist JAVA打印bool对象

时间:2017-11-20 13:47:03

标签: java arraylist boolean

getArrayStringListFalse方法应该使用bool false打印出对象

getArrayStringListTrue方法应该对bool为true的对象做同样的事情

import java.util.*;


public class Issue {

private static ArrayList<newIssue> list = new ArrayList<>();


public static ArrayList<newIssue> getArrayStringListFalse(){

    for(int i = 0; i < list.size();i++){
        if (!list.get(i).returned){
            System.out.println("["+"["+i+"] "+list.get(i)+"]");
        }else {
            System.out.println("You have no unsolved issues!");
        };
    };
    return null;
}

public static ArrayList<newIssue> getArrayStringListTrue(){

    for(int i = 0; i < list.size();i++){
        if (list.get(i).returned){
            System.out.println("["+"["+i+"] "+list.get(i)+"]");
        }else {
            System.out.println("You have no solved issues!");
        };
    };
    return null;
}



public static void removeIssue(){
    for (int i = 0; i<list.size(); i++){;
        System.out.println("["+"["+i+"] "+list.get(i)+"]");
    }
    Scanner scan = new Scanner(System.in);
    System.out.println("Which one would you like to mark as solved?");
    int choice = scan.nextInt();
    newIssue issue = list.get(choice);
    issue.returned = true;
}

public static void addIssue(){
    System.out.println("---Create a new issue---");
    System.out.println("Describe the issue: ");
    Scanner scan = new Scanner(System.in);
    String text= scan.nextLine();
    newIssue issue = new newIssue(text);
    list.add(issue);
}
}

除了im打印出“问题+ bool值”之外它正在起作用,所以它看起来像这样。 [我没有害怕]。

for循环也应该打印出真/假,这取决于方法现有的真/假对象的数量现在如果我有2个真实对象和1假并使用false方法它打印出一个假然后其他言。

希望你明白我想做什么

public class newIssue {

public String issueText;
public boolean returned = false;

public newIssue(String issueText){
    this.issueText = issueText;
}

public String toString(){
    return issueText + returned;
}



}

2 个答案:

答案 0 :(得分:1)

如果您可以使用select t.dt::date, p.name, p.team from person p, generate_series(p.start_date, p.end_date, interval '1' day) as t(dt) order by t.dt::date; java-8,则可以对其进行简化:

steams

// print all elements with returned == true    
list.stream().filter(i -> i.returned).forEach(System.out::println);

答案 1 :(得分:0)

你可以像这样修改:

public static ArrayList<newIssue> getArrayStringListFalse(){
boolean flag=false;
for(int i = 0; i < list.size();i++){
    if (!list.get(i).returned){
        System.out.println("["+"["+i+"] "+list.get(i)+"]");
        flag = true;
    }
}
if(!flag){
    System.out.println("You have no unsolved issues!");
}
return null;
}

public static ArrayList<newIssue> getArrayStringListTrue(){
boolean flag=false;
for(int i = 0; i < list.size();i++){
    if (list.get(i).returned){
        System.out.println("["+"["+i+"] "+list.get(i)+"]");
        flag = true;
    }
}
if(!flag){
        System.out.println("You have no solved issues!");
};
return null;
}