pringles19
pringles19

Reputation: 1

Deleting from ArrayList Java

I'm trying to delete an item from an array list by selecting it from a JList and clicking "Delete".

The code I have so far,

buttondeleteContact.addActionListener(new ActionListener() {    
  public void actionPerformed(ActionEvent event) {
    if (contactList.getSelectedIndex() != -1) {
      people.removeElementAt(contactList.getSelectedIndex());   
      People.remove(contactList.getSelectedIndex());
      System.out.println(People);
    }
  });

I know some things are named poorly, but people (lower case p) is the name of my DefaultListModel and People (capital P) is the name of my ArrayList. Basically, I just want to delete a block of 4 lines from an array. So, the position in the array, and the 3 after it.

Upvotes: 0

Views: 6827

Answers (4)

Summy
Summy

Reputation: 807

Try,

people.subList(index, index+4).clear()

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500105

Joachim's answer gives a good way of removing directly from an ArrayList, but I suspect you really want to remove the range directly from the model. DefaultListModel has a removeRange method:

int index = contactList.getSelectedIndex();
people.removeRange(index, index + 4);

I would expect that to have the right behaviour, removing the items from the underlying list as well. Assuming that's the case, that would be the best way of doing it I suspect. Then again, I don't know Swing very well :)

Upvotes: 1

BalusC
BalusC

Reputation: 1108672

This is an odd requirement. Deleting 3 items after it? How are they related to each other? They must be somehow related to each other. It sounds like that you have a contact list which looks like this:

List<String> contacts = new ArrayList<String>();
contacts.add("John Doe");
contacts.add("Main street 1"); // His street.
contacts.add("New York"); // His city.
contacts.add("555 123 456 789"); // His phone.
// etc..

Is this true? Then you should really consider grouping the related elements into another real-world representing object. You could create a Javabean class Contact which look like this:

public class Contact {
    private String name;
    private String street;
    private String city; // You could consider another Address class for street and city as well.
    private String phone;
    // Add/generate getters and setters.

    public Contact() {
        // Keep default constructor alive.
    }

    public Contact(String name, String street, String city, String phone) {
        this.name = name;
        this.street = street;
        this.city = city;
        this.phone = phone;
    }
}

This way you just end up with

List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact("John Doe", "Main Street 1", "New York", "555 123 456 789"));
// etc..

so that you can just remove a single real Contact by index.

You could even make it a property of People:

public class People {
    private List<Contact> contacts;
    // +getter +setter
}

Try to think OO.

Upvotes: 4

Joachim Sauer
Joachim Sauer

Reputation: 308011

While List and ArrayList don't have a direct (and accessible) removeRange() method, the need for such a method is removed by providing the subList() method.

subList() provides a view into a part of the original List. The important part to notice is that modifying the returned List will also modify the original List. So to remove the elements with the indices index to index+3, you could do this:

myList.subList(index, index+4).clear();

Note that the second argument of subList() is exclusive, so this subList() call will return a List with a size of 4.

Upvotes: 9

Related Questions