Solved

Arraylist needs to be changed

Posted on 2004-04-11
9
272 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

717 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