简体   繁体   English

Java如何检查arraylist对象并使其为true / false?

[英]Java How do i check for an arraylist object and make it true/false?

The program does one thing, depending on the users input, it removes an arraylist object and will ask you if u you want to remove another object, however, if the same object tries to be removed, i need the program to know and output 'such object does not exist', for example, 'remove "3"', then remove "3" again, the program output's "3" does not exist, the problem is that i have no idea how to implement it, what i have does not do much either. 该程序做一件事,取决于用户的输入,它将删除一个arraylist对象,并询问您是否要删除另一个对象,但是,如果同一对象试图被删除,我需要程序知道并输出'这样的对象不存在”,例如,“删除“ 3””,然后再次删除“ 3”,程序输出的“ 3”不存在,问题是我不知道如何实现它,我有什么也没有太大的作用。 My theory is that you have to use boolean to check if the arraylist object is there in the first place, if it is: remove it, if not: output "not there". 我的理论是,必须首先使用布尔值检查arraylist对象是否存在,如果存在:删除它,否则:输出“ not there”。 here's what i have: 这是我所拥有的:

String[] id1 = { "1", "studentA" };
ArrayList<String> jim = new ArrayList<String>(Arrays.asList(id1));

System.out.println("would you like to remove an id? if so type in "
        + "the id number, otherwise type: no");
Scanner sc = new Scanner(System.in);
String i = sc.next();

int position = -1;
position = jim.indexOf(sc) - 1;
if (position == -1) {
    System.out.println("not found in list");
} else {
    System.out.println("found and removed");
    jim.remove(i);

}

System.out
        .println("would you like to remove another id? if so type in "
                + "the id number, otherwise type: no");
Scanner sc2 = new Scanner(System.in);
String j = sc.next();

int position2 = -1;
position2 = jim.indexOf(sc) - 1;
if (position2 == -1) {
    System.out.println("not found in list");
} else {
    System.out.println("found and removed");
    jim.remove(j);
}

I would suggest using public boolean remove(Object o) and this will return either true or false if the element is apart of the ArrayList or not respectively. 我建议使用public boolean remove(Object o) ,如果元素分别与ArrayList分开,则返回true或false。 You can set some boolean variable to equal that, and use an if statement to output your desired response. 您可以将一些布尔变量设置为等于该变量,然后使用if语句输出所需的响应。

boolean contains(Object o) would check if the ArrayList contains the object, you could scan through the list and check if it is there or not. boolean contains(Object o)将检查ArrayList是否包含对象,您可以扫描列表并检查它是否存在。 You could also use the E get(int index) to scan through and check if the strings are equal to each other using a loop. 您还可以使用E get(int index)进行扫描,并使用循环检查字符串是否彼此相等。

If you want your program to keep on asking for user input, you need a loop, for example a while-loop , which will only terminate if the user inputs no . 如果您希望程序继续请求用户输入,则需要一个循环,例如while循环 ,该循环仅在用户输入no时才终止。 In addition to that, you can simply use List.remove() for removing the element, and inspect the return value ( true if the item was in the list and was removed) to give the correct feedback to the user: 除此之外,您可以简单地使用List.remove()删除元素,并检查返回值(如果该项在列表中并且已被删除,则为true ),以向用户提供正确的反馈:

String[] elements = { "1", "studentA" };
ArrayList<String> list = new ArrayList<String>(Arrays.asList(elements));
Scanner sc = new Scanner(System.in);

while (true) {
    System.out.println("would you like to remove an id? if so type in "
            + "the id, otherwise type: no");        
    String input = sc.next();

    if ("no".equalsIgnoreCase(input)) {
        break; // exit the loop
    }

    if (list.remove(input)) {
        System.out.println("found and removed");
    } else {
        System.out.println("not found in list");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java的IF语句中检查方法是返回true还是false? - How do I check if a method returns true or false in an IF statement in Java? 如何使用 Selenium 和 Java 在 Krypton 中检查属性 aria-disabled 的值是真还是假? - How do I check the value of the attribute aria-disabled as true or false in Krypton using Selenium and Java? 如何检查方法是否返回 true 或 false? - How do I check if a method returns a true or false? 如果找到特定数字,如何使此打印为真或假? - How do i make this print true or false if it finds a specific number? 如何检查Java中的所有布尔值是真还是假? - How to check if all boolean are true or false in Java? Java SQL +检查是/否 - java sql + check true/false 如何在java中替换“true”和“false”? - How can I replace “true” and “false” in java? 我怎样才能将arraylist对象(int,string,boolean“ false”)更改为(int,String,boolean“ true”)并将其返回到数组列表? - How can i change an arraylist object (int , string, boolean “false”) to (int,String,boolean“true”) and return it back to the array list? 如何从Java中的ArrayList中删除对象? - How do I remove an object from an ArrayList in Java? 如何更新 Java 中数组列表中对象的特定属性? - How do I update a specific property of an object in an arraylist in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM