1

I have two lists and I want to make a list of lists. For some reason after I print out each list, I get a massive one entry in the array. I am using the data coming from a hash map, and need to split the key string to get the values back, then cast the integer to a string to add to array.

    ArrayList<ArrayList<String>> listOLists = new ArrayList<ArrayList<String>>();
    ArrayList<String> singleList = new ArrayList<String>();
        for (Entry<String, Integer> entry : counts.entrySet()) {
              String val = entry.getValue().toString();               
              List<String> key = Arrays.asList(entry.getKey().split("@"));
              singleList.add(key.get(0));
              singleList.add(key.get(1));
              singleList.add(key.get(2));
              singleList.add(val);
              listOLists.add(singleList);
        }


        for(ArrayList<String> s: listOLists){
            System.out.println(s);
        }

I am getting:

[Demo-Site, 172.20.58.160, 2015-08-06, 12, Demo-Site, 10.227.145.103, 2015-08-19, 1, Demo-Site, 10.7.0.146, 2015-08-11, 3,

I want in the final output to look something like this:

[[Demo-site, 172.20.58.160, 2015-08-06, 12], [Demo-site, 10.227.145.103, 2015-08-19, 1]..etc]
2
  • Show what you are getting and how you are printing Commented Aug 25, 2015 at 16:09
  • @sᴜʀᴇsʜᴀᴛᴛᴀ updated post. Commented Aug 25, 2015 at 16:24

2 Answers 2

2

It's because you add in the same single list in every iteration. Just put singleList = new ArrayList(); in tle loop body.

Sign up to request clarification or add additional context in comments.

Comments

1

Have the last println as sopln ("[" + s + "]"); something like this should give you the output that you want.

Comments

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.