我的任務是回傳一個以 . 結尾的數字組成的陣列9。不要更改剩余數字的順序。我唯一可以使用的是 for 回圈。
我的大腦此刻有點融化,我不知道該怎么辦。
當我回傳結果時,它以int 3991599399.
目前我在考慮如何將結果放在 for 回圈之外。Сan你能幫我找到一種方法將所有帶有九的數字存盤在一個新陣列中嗎?
public int[] leavePrice9(int[] prices) {
for (int i = 0; i < prices.length; i ) {
int result = prices[i];
if (result % 10 == 9);
}
}
public static void main(String[] args) {
QuadraticEquationSolver shop = new QuadraticEquationSolver();
//Should be [399, 1599, 399]
int[] prices = new int[] {399, 1599, 399, 50, 10, 10, 70};
System.out.println(Arrays.toString(shop.leavePrice9(prices)));
}
}
uj5u.com熱心網友回復:
您可以使用以下方法getAllWithLastDigit()。它接受一個陣列int和一個數字 (0-9),并回傳一個新陣列,其中包含以該指定數字作為最后一位數字的所有元素。原始陣列的元素保持不變,因此顯然保留了順序。
import java.util.*;
public class Application {
public static void main(String[] args) {
int[] prices = new int[]{399, 1599, 399, 50, 10, 10, 70};
System.out.println(Arrays.toString(getAllWithLastDigit(prices, 9)));
}
public static int[] getAllWithLastDigit(int[] array, int lastDigit){
if(lastDigit < 0 || lastDigit > 9) throw new IllegalArgumentException("lastDigit must be between 0 and 9!");
var lst = new ArrayList<Integer>();
for (int i = 0; i < array.length; i ) {
int result = array[i];
if (result % 10 == lastDigit) lst.add(result);
}
// convert array list back to integer array
return lst.stream().mapToInt(i -> i).toArray();
}
}
預期輸出:
[399, 1599, 399]
每當我們遇到最后一位數字為 9 的元素時,這使用 anArrayList能夠動態地將元素添加到串列中。最后,我們需要將串列轉換回陣列,因為您想要回傳一個整數陣列。
您所做的只是字串連接,而不是這樣做,我們現在將元素添加到串列中,將該串列轉換回int陣列和return它。您的實作錯過了創建和回傳新陣列的部分。
編輯
這里是一個沒有使用的版本ArrayList。在這里,我們首先創建一個能夠容納最大數量結果的陣列,并向其中添加一個結果,同時counter為每個新元素增加 a。然后我們必須(可能)縮小陣列,使其只包含陣列中實際存在的元素數量。為此,我們創建了一個陣列的副本,其中包含我們結果中的許多元素。
import java.util.*;
public class Application {
public static void main(String[] args) {
int[] prices = new int[]{399, 1599, 399, 50, 10, 10, 70};
System.out.println(Arrays.toString(getAllWithLastDigit(prices, 9)));
}
public static int[] getAllWithLastDigit(int[] array, int lastDigit){
if(lastDigit < 0 || lastDigit > 9) throw new IllegalArgumentException("lastDigit must be between 0 and 9!");
// create an array which has the same size as the input, this way we guarantee that we have enough space for all result
int[] elements = new int[array.length];
// counter of how many elements are in the array
int counter = 0;
for (int i = 0; i < array.length; i ) {
int result = array[i];
if (result % 10 == lastDigit) elements[counter ] = array[i];
}
// now we need to create a new array which is exactly as long as we need it (if we don't have that already)
if(counter == array.length) return elements;
// Alternative: use Java API Arrays.copyOf(elements, counter)
return copyArray(array, counter);
}
public static int[] copyArray(int[] array, int newLength){
if(newLength < 0) throw new IllegalArgumentException("Length must not be < 0");
var copy = new int[newLength];
// make sure we don't go out of bounds because the new array could be longer than the old one
var until = Math.min(array.length, newLength);
// copy over all elements
for (int i = 0; i < until; i ) {
copy[i] = array[i];
}
return copy;
}
}
uj5u.com熱心網友回復:
這是一個基于 O/P 代碼的版本:
public int[] leavePrice9(int[] prices) {
int count = 0; // how many prices qualify?
int [] result = new int [prices.length]; // hold qualifying prices
// worst case: every price will qualify.
// result is large enough to handle worst case
for (int i = 0; i < prices.length; i ) {
if (prices[i] % 10 == 9)
result[count ] = prices [i];
}
return Arrays.copyOf (result, count);
}
public static void main(String[] args) {
QuadraticEquationSolver shop = new QuadraticEquationSolver();
//Should be [399, 1599, 399]
int[] prices = new int[] {399, 1599, 399, 50, 10, 10, 70};
System.out.println(Arrays.toString(shop.leavePrice9(prices)));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/462978.html
