• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Arraylist needs to be changed

The setActionCommand of the buttons is an audiofile, they will be listed in an arraylist as they are pressed. Example the use presses 1,2,3,4,6,12,14,20,18,19,5,7,8,11,13 and this is the order in the arraylist. The user is done, looks his choices, he can see them because they are green. Presses 20,7,19, again and the button turns back to its original color. I want 20,7,19, removed from the arraylist because that is what the used has decided.
0
Drop_of_Rain
Asked:
Drop_of_Rain
  • 4
  • 4
4 Solutions
 
objectsCommented:
use the remove() method:

list.remove(element);
0
 
mmuruganandamCommented:
You can remove the element in two ways.

One is using the index of the element other is using the element itself.

list.remove(index) or list.remove(element)


Regards,
Muruga
0
 
Drop_of_RainAuthor Commented:
Can you give me some code for this?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mmuruganandamCommented:
import java.util.ArrayList;
import java.util.List;

public class ArrayListTest
{
      private List list = new ArrayList();

      public ArrayListTest()
      {
            list.add("1");
            list.add("2");
            list.add("3");

            List l = new ArrayList();
            l.add("Test list");

            list.add(l);
      }

      public Object removeByIndex(int index)
      {
            return list.remove(index);
      }

      public boolean removeByObject(Object element)
      {
            return list.remove(element);
      }

      public static void main(String[] args)
      {
            ArrayListTest test = new ArrayListTest();

            // trying to remove new ArrayList() element
            // as we don't have the element reference.
            Object removedObject = test.removeByIndex(3);
            System.out.println(removedObject);

            // as the string doesn't have its own reference
            // use the element directly to remove.
            System.out.println(test.removeByObject("1"));
            System.out.println(test.removeByObject("2"));
            System.out.println(test.removeByObject("3"));

            // though the element is not there...
            // it won't throw any exception
            System.out.println(test.removeByObject("4"));

            // if the index is not there... it will throw
            // the IndexOutOfBoundsException

            try
            {
                  removedObject = test.removeByIndex(3);
                  System.out.println(removedObject);
            }
            catch (IndexOutOfBoundsException e)
            {
                  System.out.println("Given index not found in the list");
            }
      }
}

Hope, this helps you in understanding the removal of element in the list.

Regards,
Muruga
0
 
Drop_of_RainAuthor Commented:
How would I be able to use this for receiving the audiofiles from the setActionCommand of the buttons is an audiofile
and be able to remove them from this same class. Could you modify this code to do that.
0
 
mmuruganandamCommented:
if you are adding the setActionCommand value to your ArrayList, then On clicking of the button, check whether the actionCommand String is present in the ArrayList or not.

If it is present in the ArrayList, then you can remove that from the ArrayList.

To search whether the element is present in the ArrayList or not,

list.contains(element)  which will return boolean.


Regards,
Muruga
0
 
Drop_of_RainAuthor Commented:
This is not done yet this idea came from working on another approach and this sounded easier.

Would I just ues this .\sounds\kps\kp1.wav

I could use some a lot of help with this code.

I closed the last question, but carried it over to a new question: setActionCommand to Arraylist and remove...
0
 
mmuruganandamCommented:
Drop_of_Rain,    
What are you exactly looking for this question?

Regards,
Muruga
0
 
Drop_of_RainAuthor Commented:
I closed this question so some work doesn't get repeated. The new question is:

Modifing code with buttons
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now