将整数拆分为数组

时间:2016-05-10 19:35:48

标签: arrays

我正在尝试获取一组信息(数组为8)并将其拆分,以便将其发送到ArrayList。 如果我创建并使它们成为所有字符串,我可以运行拆分,但我正在尝试减少编码(因为必须有更好的方法)。

数组的前4个是字符串,接下来的4个是int。

ArrayList<student> studentID = new ArrayList<student>();
String[] studentInfo = {"","","",""};
int [] studentInfo2 = {0,0,0,0};
for (int x = 0; x < students.length; x++)
{
    if (x < 4)
    {    
        studentInfo = students[x].split(",");
    }
    else
    {
        **studentInfo2 = students[x]();**
    }
    for (int counter = 0; counter < 8; counter++)
    {    
        if (counter < 4) //First 4 in the array are strings
        {
            studentID.add(new student(studentInfo[counter]));
        }
        else //Everything after the first 4 are Integers
        {
            studentID.add(new student(Integer.valueOf(studentInfo[counter])));
        }
    }             
}

我无法弄清楚如何将最后4个分成数组,就像前4个那样。

编辑:

    static String[] students = "1,John,Smith,John2233@gmail.com,20,88,79,59,
    2, Becky,Jones,JonesBecky123@hotmail.com,44,90,89,44,99"

此信息正在通过管道传输到课程中,如下所示:         公立班学生         {             private String studentNumber;             private String firstName;             private String lastName;             private String emailAddress;             私人年龄;             private int []得分= {0,0,0};             private int score0;             private int score1;             private int score2;

/**
 * Constructor for objects of class studentObject
 */
public student(String theStudentNumber, String theFirstName, String theLastName, String theEmailAddress, int theAge, int theScore0, int theScore1, int theScore2)
{
    // initializing the instance variables
    studentNumber = theStudentNumber;
    firstName = theFirstName;
    lastName = theLastName;
    emailAddress = theEmailAddress;
    age = theAge;
    score[0] = theScore0;
    score[1] = theScore1;
    score[2] = theScore2;
}

然后它将调用此方法

    public void print(student printStudentInfo)
    {
        System.out.print(printStudentInfo.getStudentNumber()+"\t");
        System.out.print("First Name: " + printStudentInfo.getStudentNumber()+"\t");
        System.out.print("Last Name: " + printStudentInfo.getStudentNumber()+"\t");
        System.out.print("Age: " + printStudentInfo.getStudentNumber()+"\t");
        System.out.print("Scores : " + printStudentInfo.getStudentNumber()+"\t");
        System.out.println();

}

提前感谢您的帮助。

0 个答案:

没有答案