I need to create an array of array with Strings in java…
For example, I read a file that contain for each column a sport and a player name..like:
The goal at the end it’s for populate a list grouped in section (sports)
hockey,Wayne Gretsky
hockey,Mario Lemieux
baseball,Barry Bonds
baseball,A Rod
I need to create the [][] with this function :
public static String[][] getSportItems(int sectionCount){
String currentSection="";
int cnt=0;
try{
File file = new File(Environment.getExternalStorageDirectory()
+ "/sports.csv");
BufferedReader br = new BufferedReader(new FileReader(file));
String strLine = "";
while ((strLine = br.readLine()) != null) {
String[] data = strLine.split(";");
if(cnt>0){
}
cnt++;
}
}catch (IOException e) {
e.printStackTrace();
}
}
How I can manage that to create an array for each sport that contain the players associated with the sports.
I’m new in Java… Thanks
Bonjour Maxime,
you should better store you data in a hasmap. This allows to associate some value to another.
For instance, you could define a map that associates a sport to a list of player like this :
or if you don’t have a Player class (but better have one) :
and to put a player, do like this
and to get all people associated with a sport :
List listPlayer = mapSportToPlayer.get( sport );
and print them out on console
and then override toString in class player to provide a clear text output of your objects.
Regards, Stéphane