简体   繁体   中英

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. 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". 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. You can set some boolean variable to equal that, and use an if statement to output your desired response.

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. 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.

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 . 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:

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM