0

I have declared List<String> listOfValues = new ArrayList<String>();

It's outputs look like this:

[0.0,100.0,5.0],[1.0,200.0,8.0],[2.0,600.0,2.0],....

So for example, by calling listOFvalues.get(0) I would access 0.0,100.0,5.0

What I'm trying to do is, access and store its column values. So basically I need store 0.0,1.0,2.0 as the first array/arrayList, 100.0,200.0,600.0 as the 2nd and so on.

The problem is, so far, I'm only able to get the whole Row (with listOfValues.get()).

3 Answers 3

3

You can use the String method Split to make an array from each String element.

List<String> splittedArray = new ArrayList<>();
for(String s: listOfValues)
{
  for(String ss: s.split(",")) splittedArray.add(ss);
}

Then you can access each element through splittedArray.

Another option is to store the splitted elements into a matrix so they can be accessed as columns:

List<List<String>> matrix = new ArrayList<>();
for(String s: listOfValues)
{
  matrix.add(Arrays.asList(s.split(",")));
}
Sign up to request clarification or add additional context in comments.

5 Comments

With System.out.println(splittedArray.get(2)); I get the 3rd value only of the first Row, how can i make a for loop that goes through all columns?
@user3086819 If you chose to dump your elements to a matrix: for(List<String> row: matrix) { for(String element: row) { System.out.print(element+"\t"); } System.out.println(""); } If, on the other hand, you decided to use a plain array (splittedArray in my example): for(String element: splittedArray) System.out.println(element);
With the matrix option i see as output all rows & columns of listOfValues(need to store/save these columns somewhere) , and with the splittedArray i get the first row shown up vertically.
@user3086819 The code I posted to print the splittedArray will show you all your elements in one row, just an example of how you can access them. Both options contain already all the elements, I mean they are already stored "somewhere". If you want to access the second column of the first row of the matrix: matrix.get(0).get(1). splittedArray contains all your elements in one single row/column (as you would like to think about it).
The .get().get() made it! kudos
1

You could create your List extending ArrayList and overwriting the get method:

public class ColumnArrayList<T> extends ArrayList<String> {
    public String get(int i) {
        StringBuilder buffer = new StringBuilder();
        String delim = "";
        for(int j=0;j<super.size();j++) {
            buffer.append(delim).append(super.get(j).split(",")[i]);
            delim = ",";
        }
        return buffer.toString();
    }
}

To see it working:

public class Demo {     
    public static void main(String[] args) {
        List<String> listOfValues = new ColumnArrayList<String>();
        listOfValues.add("0.0,100.0,5.0");
        listOfValues.add("1.0,200.0,8.0");
        listOfValues.add("2.0,600.0,2.0");
        System.out.println(listOfValues.get(0));
        System.out.println(listOfValues.get(1));
        System.out.println(listOfValues.get(2));
    }
}

Will print what you want:

0.0,1.0,2.0
100.0,200.0,600.0  
5.0,8.0,2.0

Comments

0

You can use String class method split().

String temp="0.0,100.0,5.0";
String[] array=temp.split(",");

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.