gpt4 book ai didi

java - 为什么 Java 中的完整数组在部分初始化时不打印?

转载 作者:行者123 更新时间:2023-11-30 06:46:38 24 4
gpt4 key购买 nike

在下面的代码中,我调整了数组的大小,长度增加了 5 个元素。长度显示正确。但是当我运行循环来显示数组的元素时,只显示初始化的元素。如何显示所有元素?

package arraychallenge;

import java.util.*;


public class ArrayChallenge {

public static void main(String[] args) {

Scanner CountScanner = new Scanner(System.in);
System.out.println("How many integers you have in your list? ");
int count = CountScanner.nextInt();
System.out.println("Enter the integers now - ");
int[] MyArray = getIntegers(count);

System.out.println("Unsorted Array is present here:");
for (int i = 0; i < MyArray.length; i++) {
System.out.println(MyArray[i]);
}
sortArray(MyArray);
printArray(MyArray);

resizeArray(MyArray, MyArray.length, 5);
printArray(MyArray);


}

public static int[] getIntegers(int count) {
Scanner MyScanner = new Scanner(System.in);
int[] MyArray = new int[count];

for (int i = 0; i < MyArray.length; i++) {
MyArray[i] = MyScanner.nextInt();

}
return MyArray;

}

public static void sortArray(int[] MyArray) {


int temp;

for (int i = 0; i < MyArray.length; i++) {

for (int j = i+1; j < MyArray.length; j++) {

if (MyArray[i] > MyArray[j]) {

temp = MyArray[i];
MyArray[i] = MyArray[j];
MyArray[j] = temp;

}

}

}

}


public static void printArray(int[] MyArray) {
System.out.println("Sorted Array is present here:");
for (int i = 0; i < MyArray.length; i++) {
System.out.println(MyArray[i]);

}
}

public static void resizeArray(int[] MyArray, int count, int increase){
int[] tempArray = MyArray;
MyArray = new int[count+increase];
System.out.println("length of MyArray is now " + MyArray.length);
for (int i = 0; i < count; i++) {
MyArray[i] = tempArray[i];

}

}

}

最佳答案

在您的 resizeArray 中方法,您传入 MyArray 。然后创建一个大小为 5 的新数组,并将其分配给 MyArray变量,但这与您传入的不同。

阅读此答案:https://stackoverflow.com/a/40523/3887715

My attempt at a good way to visualize object passing: Imagine a balloon. Calling a fxn is like tieing a second string to the balloon and handing the line to the fxn. parameter = new Balloon() will cut that string and create a new balloon (but this has no effect on the original balloon). parameter.pop() will still pop it though because it follows the string to the same, original balloon. Java is pass by value, but the value passed is not deep, it is at the highest level, i.e. a primitive or a pointer. Don't confuse that with a deep pass-by-value where the object is entirely cloned and passed. – dhackner Oct 20 '10 at 16:38

简而言之,您调用变量 MyArrayresizeArray ,但不一样MyArray您在 main 创建。这是 resizeArray 里面的一个新变量恰好有相同的名字。所以通过这样做MyArray = new int[count+increase]; ,你并没有改变你原来的MyArray ,您正在更换一个新的。

如果你运行这个:

  public class ArrayTest {
public static void main(String[] args) {
int[] array1 = new int[]{1,2,3,4,5};
System.out.println("Print array in main 1:");
printArray(array1);

increaseArraySize(array1, 5);

System.out.println("Print array in main 2:");
printArray(array1);
}

private static void increaseArraySize(int[] array1, int size) {
int[] tempArray = array1;

array1 = new int[tempArray.length + size];

for (int i = 0; i < tempArray.length; i++) {
array1[i] = tempArray[i];
}

System.out.println("Print array in increaseArraySize:");
printArray(array1);
}

public static void printArray(int[] MyArray) {
for (int i = 0; i < MyArray.length; i++) {
System.out.println(MyArray[i]);
}
}
}

你得到:

Print array in main 1:
1
2
3
4
5
Print array in increaseArraySize:
1
2
3
4
5
0
0
0
0
0
Print array in main 2:
1
2
3
4
5

所以你可以看到第二次打印 printArray 时的 0方法被调用。

如果您更改 MyArray 的名称在resizeArray ,也许这会帮助你理解。

根据评论的建议,您可以拥有 resizeArray返回一个数组,然后将其分配回 MyArraymain() .

关于java - 为什么 Java 中的完整数组在部分初始化时不打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43579542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com