0

I have been working on this for ages now. I have an array list where many strings are in one index.

(index 0) Brand is: Nissan Colour is: Black ID is: KL1 Door is: 4
(index 1) Brand is: Ford Colour is: Red ID is: LL0 Door is: 4

I want the user to input only ID and it should remove the whole data in the index. Unfortunately Iterator won't work here. Any ideas how this can be done?

1
  • Foremost: why is this an ArrayList<String> instead of an ArrayList<Car>? That data should be a class with properties {String brand, String colour, String id, int doorCount} or something, with a toString() method that generates the text you show. It should not be a single string while you work with the data... removing on id becomes almost trivially simpler that way, too. Commented Oct 16, 2016 at 6:17

3 Answers 3

3

OOP

Use the basic object-oriented approach.

Create a class to represent each row of data, with four member fields: brand, color, id, door.

Parse each row of text to instantiate an object of that class. Collect the objects in a List.

Loop the list and interrogate each object for its id to match your desired id.

For speed in adding/deleting items, use a LinkedList rather than an ArrayList as your implementation of List.

Sign up to request clarification or add additional context in comments.

1 Comment

If one elects to use an iterator, the backing data structure matters little since the removals (at least) aren't guaranteed in anything other than O(n) (amortized).
0

You should create class that will contain those data related tor car. Take the following code as a example. I have used Java 8 feature that internally iterate over the list.

public class Main {
public static void main(String[] args){
    List<Car> list = new ArrayList<>(Arrays.asList(new Car("Nissan", "Black", "KL1", "4"),
            new Car("Ford", "Red", "LL0", "4")));

    String id = "KL1";
    list.removeIf(i -> i.getID().equals(id));
    list.forEach(System.out::println); // show the list after operation
}

static class Car {
    public final String Brand;
    public final String Colour;
    public final String ID;
    public final String Door;

    public Car(String brand, String colour, String ID, String door) {
        Brand = brand;
        Colour = colour;
        this.ID = ID;
        Door = door;
    }

    public String getBrand() {
        return Brand;
    }

    public String getColour() {
        return Colour;
    }

    public String getID() {
        return ID;
    }

    public String getDoor() {
        return Door;
    }
}

}

Update

You can also do that by doing this

    public static void main(String[] args){
    List<Car> list = new ArrayList<>(Arrays.asList(new Car("Nissan", "Black", "KL1", "4"),
            new Car("Ford", "Red", "LL0", "4")));

    String id = "KL1";
    // Following code will find and remove the match from the list.
    // this for loop do exactly what it is doing "list.removeIf(i -> i.getID().equals(id));"
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getID().equals(id)) {
            list.remove(i);
            break;
        }
    }
}

1 Comment

this is the output im getting with this code [Car@15db9742, Car@6d06d69c]
0

Storing the data in a string reminds me of good old times back in the 80s. Follow the advice of Basil Borque and then, to avoid concurrentmodificationexception, start at the end of the array. That way you can remove from a list and continue with the previous one, NOT the next one (which would cause the exception)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.