如何在 Java 中修剪整数数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22587068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trim out an array of integers in Java?
提问by Ebad Saghar
Let's that I have a number N. N will be the size of the array.
假设我有一个数字 N。 N 将是数组的大小。
int numArray [] = new numArray[N];
However, the contents of the array will hold every other number from 1 to positive N. This means that the entire size N array will not be full after that for loop. So after the for loop, I want to trim (or resize) the array so that there will no longer be any empty slots in the array.
但是,数组的内容将保存从 1 到正 N 的每隔一个数字。这意味着在 for 循环之后整个大小为 N 的数组将不会被填满。所以在 for 循环之后,我想修剪(或调整大小)数组,以便数组中不再有任何空槽。
Example :
例子 :
Let's say N = 5; That means, after the for loop, every other number from 1 to 5 will be in the array like so:
假设 N = 5;这意味着,在 for 循环之后,从 1 到 5 的每个其他数字都将在数组中,如下所示:
int arr[] = new int[N];
int arr[] = new int[N];
int arr[0]=1;
int arr[1]=3;
int arr[2]= null;
int arr[3]= null;
int arr[4]= null;
Now, I want to trim (or resize) after the for loop so that the indexes that hold null will be gone and then the array should be:
现在,我想在 for 循环之后修剪(或调整大小),以便保持 null 的索引将消失,然后数组应该是:
int arr[0]=1;
int arr[1]=3;
The size of the array is now 2.
数组的大小现在是 2。
采纳答案by Erwin Bolwidt
You can't change the size of an array in Java after it has been created. What you can do however, is to create a new array of the size that you need.
在 Java 中创建数组后,您无法更改它的大小。但是,您可以做的是创建一个您需要的大小的新数组。
Another important point is that you are creating an array of a primitive: int
. Primitives are not objects and you cannot assign the value null
to a primitive.
You need to create an array of java.lang.Integer
if you want to be able to set entries in it to null
.
另一个重要的点是您正在创建一个原始数组: int
。基元不是对象,您不能将值分配给null
基元。您需要创建数组java.lang.Integer
,如果你希望能够在它的条目集null
。
Integer[] numArray = new Integer[N];
Thats to a Java feature called auto-boxing, almost all code that works with primitive int
values, also works with Integer
values.
这就是称为自动装箱的 Java 功能,几乎所有使用原始int
值的代码也适用于Integer
值。
Steps:
脚步:
- Use
Integer[]
instead ofint[]
- Calculate the size that you need (count non-
null
entries in original array) - Allocate a new array of the size that you need
- Loop over the old array, and copy every non-
null
value from it to the new array.
- 使用
Integer[]
代替int[]
- 计算您需要的大小(计算
null
原始数组中的非条目) - 分配一个你需要的大小的新数组
- 循环遍历旧数组,并将其中的每个非
null
值复制到新数组中。
Code:
代码:
Integer[] oldArray = ...;
// Step 2
int count = 0;
for (Integer i : oldArray) {
if (i != null) {
count++;
}
}
// Step 3
Integer[] newArray = new Integer[count];
// Step 4
int index = 0;
for (Integer i : oldArray) {
if (i != null) {
newArray[index++] = i;
}
}
回答by fejese
You surely better of with some more appropriate data structure, for example a list or a set depending on what's your intention with it later. That way you don't even need to create an N sized structure just so you'd have to reduce it anyway. Rather you create an empty list and add the elements that you actually need
你肯定会更好地使用一些更合适的数据结构,例如一个列表或一个集合,这取决于你以后的意图。这样你甚至不需要创建一个 N 大小的结构,所以你无论如何都必须减少它。而是创建一个空列表并添加您实际需要的元素
回答by lreeder
You can't trim an array. The fastest approach is just to copy it into a smaller one, using System.arraycopy, which is almost always much faster than a for loop:
您不能修剪数组。最快的方法是使用 System.arraycopy 将其复制到较小的文件中,这几乎总是比 for 循环快得多:
int somesize = 5;
int[] numArray = new int[somesize];
//code to populate every other int into the array.
int[] smallerArray = new int[somesize/2];
//copy array into smaller version
System.arraycopy(numArray, 0, smallerArray, 0, somesize / 2);
回答by promanowicz
I think there is a bit shorter way to do the trimming itself. Whats left is to find the proper index.
我认为有一种更短的方法来进行修剪本身。剩下的就是找到合适的索引。
You can do:
你可以做:
int someIndex = Arrays.asList(arr).indexOf(null);
arr = Arrays.copyOfRange(arr,0,someIndex);
回答by Lijo
import java.util.Arrays;
public static void main( String[] args )
{
int[] nums2 = {9,4,1,8,4};
nums2 =Arrays.copyOf(nums2,3);
for (int i : nums2) {
System.out.print(i+" ");
}
}
//Output
//输出
9 4 1
9 4 1