需要帮助将Array转换为Java中的另一个数组

时间:2013-11-18 00:56:35

标签: java arrays

我需要组织这个数组。 String [] students={"Paul", "A", "Paul", "B", "Mike", "B", "Cindy", "A", "Cindy", "B", "Cindy", "C"}

目标是

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

请帮忙

我尝试了以下内容:

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

我无法动态分配数组名称

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作来实现您提到的功能:

1声明一个类型为List< List< String > >的列表。它用于存储学生成绩信息,如

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

2循环下面声明的学生数组,并填写每个学生的成绩,

以学生保罗为例, List<String>的学生成绩列表类型包含[Paul, A, B]

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

3将studnets-grades列表(List< List< String > >)转换为String[][]的成绩类型。

以下是使用上述3个步骤的示例:

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类型的String [] [],其在Console中打印的结果如下:

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