[英]Console output of nth element array
我有一个带有n个元素的数组,我正在尝试设置我的数组的值,以便每个元素的值都为Positon。
即,位置0处的第一个元素为0,位置2处的第二个元素,依此类推,直到位置n-1处的第n个元素,其值为n-1。
最后,我将在控制台上提供阵列的内容。
好吧,我已经设置了正确的值,但是我无法在控制台上显示。例如,如何显示位置“ n-1”的值为“ n-1”?
这是我到目前为止所做的:
public void exercise1(Integer n){
int[] arrayA = new int[n];
int counter;
for(counter=0; counter<arrayA.length; counter++){
arrayA[counter]=counter;
}
}
提前致谢:)
使用System.out.println和以下代码:
System.out.println(Arrays. toString(your_array_name_here));
将此添加到您的循环
System.out.println(arrayA[counter]);
我认为HélioSantos所说的是对的,对我有用:
public class koponk {
public static void main(String[] args) { exercise1(10); } public static void exercise1(Integer n) { int[] arrayA = new int[n]; int counter; for (counter = 0; counter < arrayA.length; counter++) { arrayA[counter] = counter; System.out.print(arrayA[counter] + " "); } }
}
public class apples{
public static void main(String[] args) {
exercise(4);
}
public void exercise(Integer n){
int[] arrayA = new int[n];
int counter;
for(counter=0; counter<arrayA.length; counter++){
arrayA[counter]=counter;
}
System.out.print("[");
for(counter=0; counter<arrayA.length; counter++){
if(counter == n-1){
System.out.print(arrayA[counter]);
}else{
System.out.print(arrayA[counter] + ", ");
}
}
System.out.println("]");
}
}
伙计们,在您提出建议后,我做了类似的事情,并且奏效了。 非常感谢你:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.