Skip to content
Advertisement

Print a string from ArrayList of String[] per Index?

How to print ArrayList of String[] into String per index with expected Output like this

Nama: John
Kimia: 10
biologi: 9
fisika: 8

Nama: Peter
Kimia: 7
biologi: 8
fisika: 10

Nama: Steve
Kimia: 6
biologi: 9
fisika: 8

Here is the code and how to print it?

public static String dataSiswa(ArrayList<String[]> list_Siswa){

    for (String[] dataSiswa : list_Siswa){
        String nama = dataSiswa[0];
        String kimia = dataSiswa[1];
        String biologi = dataSiswa[2];
        String fisika = dataSiswa[3];

        data = "Nama: " + nama;
    }
    return data;
}

Advertisement

Answer

You were pretty close to it:

public static String dataSiswa(ArrayList<String[]> list_Siswa){

    String data = "";
    for (String[] dataSiswa : list_Siswa){
        data += "Nama: " + dataSiswa[0] + "n" +
                "Kimia" + dataSiswa[1] + "n" +
                "Biologi" + dataSiswa[2] + "n" +
                "Fisika" + dataSiswa[3] + "nn";
    }
    return data;
}

This generates your String to be outputted. Printing:

System.out.print(yourArrayList);

Just don’t forget to initialize yourArrayList prior of the method call.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement