Solved

Declaring unknown ArrayList

Posted on 2003-11-28
4
1,859 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 video teaches viewers about errors in exception handling.
Suggested Courses

623 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