[英]How to trim out an array of integers in Java?
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.但是,数组的内容将保存从 1 到正 N 的每隔一个数字。这意味着在 for 循环之后整个大小为 N 的数组将不会被填满。 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.所以在 for 循环之后,我想修剪(或调整大小)数组,以便数组中不再有任何空槽。
Example :例子 :
Let's say N = 5;假设 N = 5; That means, after the for loop, every other number from 1 to 5 will be in the array like so:这意味着,在 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。
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);
You can't change the size of an array in Java after it has been created.在 Java 中创建数组后,您无法更改它的大小。 What you can do however, is to create a new array of the size that you need.但是,您可以做的是创建一个您需要的大小的新数组。
Another important point is that you are creating an array of a primitive : int
.另一个重要的点是您正在创建一个原始数组: int
。 Primitives are not objects and you cannot assign the value null
to a primitive.基元不是对象,您不能将值null
分配给基元。 You need to create an array of java.lang.Integer
if you want to be able to set entries in it to null
.如果您希望能够将其中的条目设置为null
,则需要创建一个java.lang.Integer
数组。
Integer[] numArray = new Integer[N];
Thanks to a Java feature called auto-boxing , almost all code that works with primitive int
values, also works with Integer
values.得益于称为auto-boxing的 Java 功能,几乎所有使用原始int
值的代码也适用于Integer
值。
Steps:脚步:
Integer[]
instead of int[]
使用Integer[]
而不是int[]
null
entries in original array)计算您需要的大小(计算原始数组中的非null
条目)null
value from it to the new array.循环遍历旧数组,并将其中的每个非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;
}
}
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);
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.这样你甚至不需要创建一个 N 大小的结构,所以你无论如何都必须减少它。 Rather you create an empty list and add the elements that you actually need而是创建一个空列表并添加您实际需要的元素
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.