Solved

Declaring unknown ArrayList

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 52
eclipse apache tomcat admin console 52 147
Which non-HTML GUI front end to use with Java? 3 50
Print Rhino Java Array in Javascript 1 30
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…
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 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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

680 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