0

I've got an ArrayList that looks like this:

ArrayList<String> item;

item [0] --> boom

item [1] --> pow

item [2] --> bang

item [3] --> zing

Now what I'm trying to do is take each item and create an empty ArrayList of strings with that item's name. For instance, the result would be

ArrayList<String> boom; 

ArrayList<String> pow; 

ArrayList<String> bang;

ArrayList<String> zing;

Sorry if this is a simple answer, but I'm still learning.

6
  • 4
    Please post real code. What type is item? Can you post it? Commented May 25, 2015 at 19:28
  • What would you do with all these variables once you make them??? Wouldn't you have to type their names? Commented May 25, 2015 at 19:29
  • Would it not be easier just to start with a string array and create arraylists from there? Commented May 25, 2015 at 19:34
  • Not sure I follow. Basically, It's for a website scraping project. The code goes to a site, captures the headings in different paragraphs, and then needs to create an Arraylist with each heading name in order to store the paragraph in. I can already get the heading titles, but for organization purposes i need the heading titles in the ArrayList to become new ArrayList titles so I know where to assign each paragraph. @ElliottFrisch - ArrayList<String> item; Commented May 25, 2015 at 19:38
  • 1
    Why not use a e.g. Map<String,List<String>> for this task? The key could be the heading and the words of the paragraph could be stored inside List<String>. Iterating over the keys/ headings is very easy. Commented May 25, 2015 at 19:41

2 Answers 2

2

If I understand your question, then you might use a Map<String, ArrayList<String>> map, check if the heading is already present (and if it isn't create a new ArrayList<>). Something like,

Map<String, List<String>> headings = new HashMap<>();
// perform processing in a loop, for each heading...
String heading = "Example";
String content = "Body";
// ...
if (!headings.containsKey(heading)) {
    headings.put(heading, new ArrayList<>());
}
List<String> bodies = headings.get(heading);
bodies.add(content);
// .. iterate heading

or you might prepopulate the headings map like

List<String> nameList = Arrays.asList("boom", "pow", "bang");
Map<String, List<String>> headings = new HashMap<>();
for (String name : nameList) {
    headings.put(name, new ArrayList<>());
}
// ... 
Sign up to request clarification or add additional context in comments.

Comments

1

In programming language variable names is used to refers a stored value in computer memory. So a variable name can be considered as a key to access the value stored in computer memory. The standard data structure Map have the similar key value structure. So we can user Map here - "boom" as a key and new ArrayList<String>() as an value.

Suppose you have all the names (that is boom, pow, bang) in the nameList -

ArrayList nameList = new ArrayList(){{
    add("boom");
    add("pow");
    add("bang");
}};  

Now you want to create 3 ArrayList of Stirng by the name given in nameList. So you put them in a Map<String, List<String> like this -

Map<String, List<String> > vars  = new HashMap<String, List<String>>();

for(int i=0; i<nameList.size(); i++){

   String key = nameList.get(i);
   List<String> value = new ArrayList<String>();
   vars.put(key, value);
}   

The complete cod can be -

import java.util.*;

public class ArrayListFromNameList {

    public static void main(String[] args){

        List<String> nameList = new ArrayList<String>(){{
            add("boom");
            add("pow");
            add("bang");
        }};  

        Map<String, List<String> > vars  = new HashMap<String, List<String>>();

        for(int i=0; i<nameList.size(); i++){

           String key = nameList.get(i);
           List<String> value = new ArrayList<String>();
           vars.put(key, value);
        } 

    }

  /* Use the Map vars like this -
  * vars.get("boom") --> will reuturns you an ArrayList<String>();
  * similarly vars.get("pow") --> will returns you an ArrayList<String>();
  */
}

1 Comment

Thank you. This is what I was looking for. Since this is a project on web scraping, and I will need to visit more than one page, I probably won't know the names of the headings without printing them out to the screen. I know that hashmaps don't have an index (or so I thought), but how would I access them without knowing the heading names?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.