Solved

Arraylist needs to be changed

Posted on 2004-04-11
9
239 Views
Last Modified: 2012-06-27
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
Comment
Question by:Drop_of_Rain
  • 4
  • 4
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 10803481
use the remove() method:

list.remove(element);
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 300 total points
ID: 10803560
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
 

Author Comment

by:Drop_of_Rain
ID: 10803619
Can you give me some code for this?
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 300 total points
ID: 10803643
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Drop_of_Rain
ID: 10803657
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
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 300 total points
ID: 10803668
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
 

Author Comment

by:Drop_of_Rain
ID: 10803693
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10803755
Drop_of_Rain,    
What are you exactly looking for this question?

Regards,
Muruga
0
 

Author Comment

by:Drop_of_Rain
ID: 10803795
I closed this question so some work doesn't get repeated. The new question is:

Modifing code with buttons
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now