Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Declaring unknown ArrayList

Posted on 2003-11-28
4
Medium Priority
?
1,861 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 750 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

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

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 …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

783 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