Elvis
Elvis

Reputation: 255

Need help to convert Array into another array in Java

I need to organize this array. String [] students={"Paul", "A", "Paul", "B", "Mike", "B", "Cindy", "A", "Cindy", "B", "Cindy", "C"}

The target is

String [][] grades={{"Paul", "A", "B"} , {"Mike", "B"} , {"Cindy", "A", "B", "C"}}

Please help

I tried the following:

int numberOfFirstGuyElements = StringUtils.countMatches(students, students[0]);
for (int i = 0; i < numberOfFirstGuyElements*2; i++) {
String s+i = students[i] + "," students[i+1];
}

I cannot dynamically assign array names

Upvotes: 1

Views: 125

Answers (1)

Mengjun
Mengjun

Reputation: 3197

You can do the following things to implement the function you mentioned:

1 Declare a List, whose type is List< List< String > >. It is used to store students-grades information, like

  [[Paul, A, B], [Mike, B], [Cindy, A, B, C]]

2 Loop the students Array you declared below and populate every student's grades,

Take student Paul for example, the student-grade list type of List<String> contains [Paul, A, B]

String [] students={"Paul", "A", "Paul", "B", "Mike", "B", "Cindy", "A", "Cindy", "B", "Cindy", "C"}

3 Convert the studnets-grades List (List< List< String > >) to grades type of String[][] .

Below is an example using the above 3 steps:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Example {

public static void main(String[] args) {
    String [] students={"Paul", "A", "Paul", "B", "Mike", "B", "Cindy", "A", "Cindy", "B", "Cindy", "C"};

    String[][] grades =  convertArray(students);

    System.out.println("Student - Grades iformation is as follows: ");

    for(String[] stuGrade :  grades)
    {
        System.out.println(Arrays.toString(stuGrade));
    }
}

public static String[][] convertArray(String[] students)
{
    List<List<String>> stuGradeList = new ArrayList<List<String>>();

    List<String> tempStuGrade = new ArrayList<String>();
    for(int i =0; i< students.length; i++)
    {
        /*
         * Handles Name information
         */
        if(i%2 == 0)
        {
            if(!tempStuGrade.contains(students[i]))
            {
                if(tempStuGrade.isEmpty())
                {
                    tempStuGrade.add(students[i]);
                }else
                {
                    stuGradeList.add(new ArrayList<String>(tempStuGrade));

                    /*
                     * Student changed
                     * 
                     * Clear the original list and put the new Student name into temp list
                     */
                    tempStuGrade.clear();
                    tempStuGrade.add(students[i]);
                }
            }

        }else
        {
            //Add Grade to list directly
            tempStuGrade.add(students[i]);


            if(i == students.length-1)
            {
                stuGradeList.add(new ArrayList<String>(tempStuGrade));
            }
        }
    }

    String[][] grades   = new String[stuGradeList.size()][];

    /*
     * Put value of List<List<String>> into 2D Array (String[][])  
     */
    for(int i = 0;i <stuGradeList.size(); i++)
    {
        grades[i] = new String[stuGradeList.get(i).size()];
        grades[i] = stuGradeList.get(i).toArray(grades[i]);
    }

    return grades;
}
}

Grades type of String[][], its result printed in Console is as follows:

Student - Grades iformation is as follows: 
[Paul, A, B]
[Mike, B]
[Cindy, A, B, C]

Upvotes: 2

Related Questions