-2

I'm successfully getting the values from CSV file in to List<String[]>, but having problem in moving values from List<String[]> to String[] or to get single value from List. I want to copy these values in to string array to perform some functions on it.

My values are in scoreList

final List<String[]> scoreList = csvFile.read();

Now I want to get single value from this scoreList. I have tried this approaches but could not get the value

String[] value=scoreList.get(1);
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
hatib abrar
  • 126
  • 3
  • 14
  • 1
    as long as `scoreList.size() > 1` then `String[] value=scoreList.get(1);` should works ... so where is the problem? ... of course it would be a string array ... Do you know such java's basics like: how to work with arrays? or how to obtain an element from an array? – Selvin Nov 21 '16 at 17:07
  • You aren't parsing a csvFile here, you are just reading it. Make sure scoreList is actually what you expect it to be. If it is, then `scoreList.get(n)` will get you the n'th String array in the list and `scoreList.get(n)[x]` will get you the x'th String in the n'th array in the list. – zgc7009 Nov 21 '16 at 17:07
  • @Selvin yes i know how to work with array and get value from array but i was just confused with this List containign array in it – hatib abrar Nov 21 '16 at 17:23
  • I don't get it... What is confusing you... It like matrix or two dimensional array. Which is obvious choice as you have rows and columns... Look at @zgc7009 comment. – Selvin Nov 21 '16 at 17:38

2 Answers2

0

You want a single value but you are declearing an array an you are tring to assign string to string array. If you want a single value, try this;

String x = scoreList.get(1);

or

if you want to convert listarray to string array try this;

String[] myArray = new String[scoreList.size()];

for(int i=0; i<scoreList.size();i++)
{
    myArray[i]=scoreList.get(i);
}
F K
  • 51
  • 9
0

Suppose you want to collect values of the 2nd column (index 1) then you can try this

// Collect values to this list.
List<String> scores = new ArrayList<String>();

final List<String[]> scoreList = csvFile.read();

// For each row in the csv file
for (String [] scoreRow : scoreList ) {

     // var added here for readability. Get second column value
     String value = scoreRow[1];

     scores.add(value);
} 
Sagar U
  • 76
  • 4