Solved

Arraylist needs to be changed

Posted on 2004-04-11
9
255 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
eclipse java  build path 6 56
session migration servlets 2 35
javap bin 2 34
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

815 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

10 Experts available now in Live!

Get 1:1 Help Now