这是我到目前为止所做的:
import java.util.*;
public class SArray {
private int[] array;
public SArray(int a[]) {
this.array = a;
}
public String toString() {
String arrayString = "";
int i = 0;
while (i < array.length) {
arrayString = arrayString + array[i];
i++;
}
return arrayString;
}
public static void main(String[] args) {
SArray tester = new SArray(new int[] { 23, 17, 5, 90, 12, 44, 38, 84,
77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49 });
tester.toString();
}
}
我查找了如何通过构造函数传递数组,这就是我提出的但是没有值实际进入数组而我想知道为什么?
答案 0 :(得分:2)
这些值会进入数组,但您无法使用它们。您可能希望/需要显示值,因此请使用System.out.println
public static void main(String[] args) {
SArray tester = new SArray(new int[] {23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49});
//now you're doing something with the value of toString
System.out.println(tester);
}
答案 1 :(得分:1)
阵列就在那里......看:
public static void main(String[] args) {
NewClass tester = new NewClass(new int[]{23, 17, 5, 90, 12, 44, 38, 84,
77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49});
for(int i = 0; i < tester.array.length; i++){
System.out.println(tester.array[i]);
}
}
这是输出:
23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49,