[英]Basic Java? Arguments of an array?
So I'm currently learning Java and attempting a basic program.所以我目前正在学习 Java 并尝试一个基本的程序。 I'd like to create a new array which is 1 size larger than my old array and make the arguments of the new array the same as the old array other than the last argument which I would like to define.我想创建一个比旧数组大 1 大小的新数组,并使新数组的参数与旧数组相同,而不是我想定义的最后一个参数。 What is currently wrong with my current code?我当前的代码目前有什么问题? (I have defined q earlier in the code) (我之前在代码中定义了 q)
int p = c.length;
p++;
int[] d = new int[p];
for (int n=0; n<d.length-1; n++)
{
d[n] = c[n];
}
d[d.length-1]=q;
Works perfectly for me:非常适合我:
public class ArrayEx {
public static void main(String[] args) {
int[] arr1 = {10, 20, 30, 40, 50};
int length1 = arr1.length;
length1++;
int[] arr2 = new int[length1];
for (int i = 0; i < arr2.length - 1; i++) {
arr2[i] = arr1[i];
}
arr2[arr2.length - 1] = 10;
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
}
}
This prints这打印
10
20
30
40
50
10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.