Solved

Declaring unknown ArrayList

Posted on 2003-11-28
4
1,846 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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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