Solved

Arraylist needs to be changed

Posted on 2004-04-11
9
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

738 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