views:

310

answers:

4

Hi, 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 is

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. Thanks for any help :)

+2  A: 

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

Jon Skeet
haha.....the names caused me a lot of pain before i realised my mistake :)but yeah, eclipse doesn't seem to like removeRange. I get the error "The method removeRange(int, int) from the type ArrayList<String> is not visible"
pringles19
Oops. My bad - will edit.
Jon Skeet
Instead of removing item 4 4 times, sometimes it is easier to understand if you remove backwards; items 7, 6, 5, 4.
rsp
@rsp: I think I would personally find that harder to understand. It's more efficient, mind you :)
Jon Skeet
In Java it's `people.subList(index, index+4).clear()`. The `subList()` method make dedicated methods that act only on a selected range unnecessary.
Joachim Sauer
@Joachim: Good spot. Had entirely forgotten about sublists. Will edit.
Jon Skeet
+4  A: 

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.

BalusC
+4  A: 

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.

Joachim Sauer
+1  A: 

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

Summy