0

My goal is to concat the value all together into one string from the value in different list. My string looks like this: '0 0 0'. The first 0 represents 'second', the second 0 represents 'minute', and the third 0 represents 'hour'. I've created three different list to contain the values. First one is a second list, second one is a minute list, third one is a hour list. After I create all three lists, I put those three lists into one master list. I'm trying to loop through the master list, then assign the value into those positions to create the string based on the size of those three list. Those three list size are identical.

import java.util.*;
   public class multipleList{
   public static void main(String[] args){
   ArrayList<String> seconds=new ArrayList<String>();
   ArrayList<String> minutes=new ArrayList<String>();
   ArrayList<String> hours=new ArrayList<String>();
   ArrayList<String> masterList=new ArrayList<String>();

   seconds.add("10");
   seconds.add("20");
   seconds.add("30");
   minutes.add("15");
   minutes.add("16");
   minutes.add("17");
   hours.add("2");
   hours.add("3");
   hours.add("4");

   masterList.addAll(seconds);
   masterList.addAll(minutes);
   masterList.addAll(hours);

       for(String subList:masterList){
          System.out.println(subList);
       }

}

The output from console print is 10, 20, 30, 15, 16, 17, 2, 3, 4. Now my problem is how can I assign those values into the string after loop through the master list such as the first string will be '10 15 2' (format is 'second minute hour'), the second string will be '20 15 3' and the third string will be '30 17 4'.

3
  • Why do you need to use collections at all? You can just simply concatenate the strings and return them in your desired format..... Commented Mar 29, 2013 at 5:19
  • This is just the sample how I add the element into the list. Originally, those values are retrieved from the user input and will be added it into the list dynamically. So users will enter those values for second, minute, and hour. Then I need to create a string combining those three values together. Commented Mar 29, 2013 at 5:28
  • Then you can use R.J's answer...... Commented Mar 29, 2013 at 5:31

2 Answers 2

2

Replace this code snippet

masterList.addAll(seconds);
masterList.addAll(minutes);
masterList.addAll(hours);

with this code snippet:-

// Assuming the size of all the 3 lists(hr, min, sec) are the same
for(int i=0;i<hours.size();i++){
    String finalStr = hours.get(i) + " " + minutes.get(i) + " " + seconds.get(i);
    masterList.add(finalStr);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this?

StringBuilder b = new StringBuilder();
for (int j = 0; j < seconds.size(); ++j) {
  for (int i = j; i < masterList.size(); i += seconds.size()) {
     b.append(masterList.get(i)).append(" ");
  }
  b.append('\n');
}

System.out.println(b);

The first loop divides the masterList into seconds.size() groups. We loop through each group with the nested (inner) loop. The elements of a single group are seconds.size() distance apart from each other, so we increment by seconds.size().

Warning: The three lists must be of equal size, and the masterList should be of size

seconds.size() * 3.

1 Comment

Three lists will be of equal size for sure. I will definitely try that and update the result. Thanks

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.