如何查找 int 数组的索引是否以特定数字结束 Java
How do find if an index of int array ends on a specific digit Java
我有一项任务是 return 一组以 9
结尾的数字。不要改变剩余数字的顺序。
我唯一可以使用的是 for 循环。
一时脑袋有些发麻,不知如何是好。
当我 return 结果时,它 return 作为 int
3991599399
。
目前我在考虑如何将结果取出for循环。
你能帮我找到一种方法将所有带 9 的数字存储在一个新数组中吗?
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)));
}
}
您可以使用以下方法getAllWithLastDigit()
。它采用 int
和一个数字 (0-9) 的数组,return 是一个新数组,其中包含以该指定数字为最后一位的所有元素。原始数组的元素保持不变,因此显然保留了顺序。
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]
这使用 ArrayList
来能够在我们遇到最后一位数字为 9 的元素时动态地将元素添加到列表中。最后,我们需要将列表转换回数组,如您所愿 return 整数数组。
您所做的只是字符串连接,而不是这样做,我们现在将元素添加到列表中,将该列表转换回 int
数组并 return
它。您的实现遗漏了创建新数组和 returned.
的部分
编辑
这里是一个没有使用 ArrayList
的版本。在这里,我们首先创建一个能够容纳最大数量结果的数组,并向其中添加一个结果,同时为每个新元素增加一个 counter
。然后我们必须(可能)缩小数组,使其只包含数组中实际元素的数量。为此,我们创建了一个数组的副本,无论结果中有多少元素。
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;
}
}
这是一个基于 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)));
}
}
我有一项任务是 return 一组以 9
结尾的数字。不要改变剩余数字的顺序。
我唯一可以使用的是 for 循环。
一时脑袋有些发麻,不知如何是好。
当我 return 结果时,它 return 作为 int
3991599399
。
目前我在考虑如何将结果取出for循环。 你能帮我找到一种方法将所有带 9 的数字存储在一个新数组中吗?
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)));
}
}
您可以使用以下方法getAllWithLastDigit()
。它采用 int
和一个数字 (0-9) 的数组,return 是一个新数组,其中包含以该指定数字为最后一位的所有元素。原始数组的元素保持不变,因此显然保留了顺序。
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]
这使用 ArrayList
来能够在我们遇到最后一位数字为 9 的元素时动态地将元素添加到列表中。最后,我们需要将列表转换回数组,如您所愿 return 整数数组。
您所做的只是字符串连接,而不是这样做,我们现在将元素添加到列表中,将该列表转换回 int
数组并 return
它。您的实现遗漏了创建新数组和 returned.
编辑
这里是一个没有使用 ArrayList
的版本。在这里,我们首先创建一个能够容纳最大数量结果的数组,并向其中添加一个结果,同时为每个新元素增加一个 counter
。然后我们必须(可能)缩小数组,使其只包含数组中实际元素的数量。为此,我们创建了一个数组的副本,无论结果中有多少元素。
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;
}
}
这是一个基于 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)));
}
}