当前位置:首页>正文

java如何返回一个int数组? 这样返回一个数组对吗(JAVA)

2023-05-21 23:53:51 互联网 未知

java如何返回一个int数组?

返回数组,直接写一个方法就可以了呀

private static int[] getArray() {
int[] tmp = new int[10]
for (int i = 0 i < tmp.length i ) {
tmp[i] = i
}
return tmp
}

这样返回一个数组对吗(JAVA)

这样肯定不行了。。。构造方法里面不知道每个方法那个先,那个后,所以可能会出错,修改如下:
public class Student {
String ID

String name

int maxCourse

public Student(String ID, String name, int maxCourse) {
this.ID = ID
this.name = name
this.maxCourse = maxCourse
score = new int[maxCourse 1]
}

int[] score = null

public String getID() {
return ID
}

public String getName() {
return name
}

public int[] getScore() {
setScore(name)
return score
}

public void setID(String ID) {
this.ID = ID
}

public void setName(String name) {
this.name = name
}

public void setScore(String name) {
for (int i = 0 i < maxCourse i ) {
score[i] = (int) (100 * Math.random())
score[maxCourse] = score[i]
}
}

public static void main(String args[]){
Student s = new Student("1", "Zhang San", 5)
int[] ary = s.getScore()
for(int value: ary){
System.out.println(value)
}
}
}