user2042881
user2042881

Reputation: 125

arraylist value into string value

I have a arraylist containing values like [ addidas 5 100 , nike 10 300 , woodland 4 800] so if i print am gettinh result as

value1 :: adidas
value2 :: 5
value3 :: 100

result has to be like

value1 :: adidas,nike,woodland
value2 :: 5,10,4
value3 :: 100,300,800
ArrayList<String> array;

int j = 0;

String[] parts;
String value1;
String value2;
String value3;

for (j = 0; j <= array.size(); j++) {

    parts = array.split(" ");

    value1 = parts[0];
    value2 = parts[1];
    value3 = parts[2];

    System.out.println("value1::" + value1);
    System.out.println("value2::" + value2);
    System.out.println("value3::" + value3);

}

Upvotes: 0

Views: 110

Answers (1)

Pshemo
Pshemo

Reputation: 124225

Since this looks like homework there will be no code here.
There is one error in your code and few in logic.

  • split can be invoked on String so first you should get that string from your array list
  • you said that you want to print all elements of the same category (value1, value2, value3) in same line but your loop prints one value of each category in each iteration. To solve this problem you should store split values in separated lists and print them after loop ends.

Upvotes: 4

Related Questions