[英]Array Not Printing - Arrays.toString(nums)
我不確定如何打印數組; 我嘗試使用 Arrays.toString(nums),但它仍然不起作用。 我需要做什么來解決這個問題?
導入 java.util.Arrays;
公共 class 傳遞數組 {
公共 static 無效傳遞數組(int [] nums){
int arr[] = {6, 3, 8, 89, 34, 89, 132, 76, 34, 89, 11, 9, 19} ;
System.out.println(Arrays.toString(nums));
int[] temp = new int[nums.length]; //create temporary array, temp, set the length of it equal to the original array
for(int i = 0; i < nums.length; i++) {
temp[i] = nums[i]; //set every value at temp equal to every number at i
}
int index = 0; //create integer index to keep track of index
for(int i = 0; i < temp.length; i++) {
if(temp[i] % 2 ==0) { //if number at i divided by 2 yields remainder of 0
nums[index] = temp[i]; //set number at index equal to temp at i, because temp at i is even
index++; //Increment of index++ means index will increase by one each loop
//This is done because if this is not done, only the first number will be replaced
}
}
for (int i = 0; index < temp.length; index++) {
if (temp[i] % 2 != 0) { //if number at i divided by 2 does not yield remainder of 0
nums[index] = temp[i]; //add the temp value to nums at index
index++; //Increment of index++ means index will increase by one each loop
}
}
}
}
因為您沒有將數組命名為 nums。 您將其更改為 arr[]。 聲明數組如下:
int nums[] = {6, 3, 8, 89, 34, 89, 132, 76, 34, 89, 11, 9, 19} ;
System.out.println(Arrays.toString(nums));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.