Solved

Declaring unknown ArrayList

Posted on 2003-11-28
4
1,848 Views
Last Modified: 2011-09-20
I'm stuck with the following:

In normal circumstances, declaring an ArrayList in java will  be:
ArrayList mylist = new ArrayList();

But I want to declare something like that:
String x = "apple";
The ArrayList will have to be named as the value of String x, ie "apple".
The value of x will only be known during runtime, thus unable to declare it
as : ArrayList apple = new ArrayList();

Anyone has solution to this problem, please kindly post here.
Thanks a lot.
0
Comment
Question by:xinying
  • 2
4 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 9841166
The closest I can offer would be by using a level of indirection connecting whichever name you get to an array list, fx by using a hashtable with the string as key and the arraylist as the data.

It you describe your reason for wanting this it will probably be possible to find a better solution

regards JakobA.

0
 
LVL 92

Expert Comment

by:objects
ID: 9841179
> The ArrayList will have to be named as the value of String x, ie "apple".

Why exactly, how will it then be accessed?
0
 
LVL 15

Accepted Solution

by:
JakobA earned 250 total points
ID: 9841278
fx something like:

import java.util.Hashtable;
import java.util.ArrayList;

public class NamedLists {
   
    private Hashtable ht = new Hashtable();
   
    public void newList( String name ) {
            // create ArrayList with that name
        if ( ht.containsKey( name ) ) {
            // error condition. the key exist already
        } else {
            ht.put( name, new ArrayList() );  // create new list
        }
    }// endmethod newList
   
    public ArrayList get( String name ) {
            // find ArrayList with that name
        return (ArrayList)ht.get(name);  // returns null if no such key
    }// endmethod get
   
    public void remove( String name ) {
            // delete list with that name
        if ( ht.containsKey( name ) ) {
            ht.remove( name );  // remove that list
        } else {
            // error condition. the key do not exist
        }
    } //endmethod remove
   
} // endclass NamedLists

class TestLists {

    public static void main( String[] args ) {

        NamedLists nl = new NamedList();
        nl.newList("name1");
        nl.newList("name2");    // make 2 named arraylists
       
            // now (nl.get("name1")) is the ArrayList with the name "name1"
        (nl.get("name1")).add("olsen");    //add to the list at end
        (nl.get("name1")).add(1,"olsen2"); //add to the list at index
       
            // and (nl.get("name2")) is the ArrayList with the name "name2"
        if ( (nl.get("name2")).isEmpty() ) { // check if empty
            (nl.get("name2")).add(0,"name2data"); //add to the list at start
        }
       
        //etc.
    } //endmethod main
   
} //endclass TestLists

Note that it is up to you to catch and handle errors you might make with wrong names, etc.
0
 
LVL 7

Expert Comment

by:yoren
ID: 9841282
This is an odd request, and I don't believe it's possible at run time. You may have a fundamental design problem -- you're probably missing a method or two. If you explain the background of the problem, folks may be able to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

929 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