import java.util.Arrays;
public class example
{
private int numbers[]; //instance variable
private String result;
public example()
{
numbers = new int[10];
Arrays.fill(numbers, 42);
}
public example(int[] array)
{
numbers = Arrays.copyOf(array, array.length);
}
//get and set methods
public void setNumbers (int numbers)
{
//setMethod
this.numbers = new int[numbers];
}
public int [] getNumbers()//get method
{
return numbers;
}
public String toString()
{
String result = new String();
int i;
for (i = 0; i < numbers; i++)
result = result + list[i] + "\n";
}
}
嘿傢伙,所以我遇到了我的toString()
方法的麻煩。我的toString()
方法應該返回一個String
,其中包含我的實例數組元素與中間的製表符。因此,對於這個是我需要幫助寫TAB toString方法
當我編譯爲我的for(i = 0; ..語句 – user1804737
'數字'是一個數組,因此您需要使用'numbers.length' – mecsco
只是增加了第二個操作數的長度 –