如何从整型数组 (Java) 中删除空元素?

我有一个包含元素的数组。我想删除这些元素并减小数组的大小。int0

我在从 Array(Java) 中删除空元素中阅读了数组的示例,并将其应用于我的情况:String

int[] in1 = {0, 0, 2, 0, 3};
int[] in1 = Arrays.stream(in1).filter(x -> x != 0).toArray(int[]::new);

很遗憾,我收到一个错误:

类型中的方法不适用于参数toArray()IntStream(int[]::new)

我的问题是:

  1. 我怎样才能实现我的目标?

  2. 为什么我不能像过滤数组一样过滤我的数组?intString


炎炎设计
浏览 266回答 3
3回答

小怪兽爱吃肉

对于基元,不要向 提供参数:int[]toArray()in1&nbsp;=&nbsp;Arrays.stream(in1).filter(x&nbsp;->&nbsp;x&nbsp;!=&nbsp;0).toArray();另请注意,您不再使用前缀,因为您已经定义了变量in1int[]in1它的工作方式与另一个问题不同的原因是,Arrays.stream(int[])&nbsp;返回了 一个具有&nbsp;toArray()&nbsp;版本的版本,该版本返回一个 .IntStreamint[]用Arrays.stream(new&nbsp;String[]{"This","",&nbsp;"will",&nbsp;"",&nbsp;"",&nbsp;"work"})将调用数组流(T[] 数组),它:返回以指定数组作为其源的顺序。Stream或 .然后使用来自类将调用此版本,该版本接受 ,该版本会将其转换为特定类型的 .(不接受来自任何参数的方法返回Stream<String>toArray()StreamIntFunctionArraytoArray()StreamObject[])

慕桂英3389331

众所周知,基元类型在java中不是对象,对于它们,类中有重载方法。假设这里只是考虑.如果我们通过,那么被调用并返回对象。如果你去看类,那么你会发现只有一个方法,它不接受任何参数。stream()Arraysintint[]stream(int[] array)IntStreamIntStreamtoArray所以,我们不能做.toArray(int[]::new)int[] in1 = {0, 0, 2, 0, 3}; int[] in2 = Arrays.stream(in1).filter(x -> x != 0).toArray();但是对于任何引用类型数组,我们可以转换为特定类型。例如String[]&nbsp;str2&nbsp;=&nbsp;{"a","b",null,"c"}; &nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;strArr&nbsp;=&nbsp;Arrays.stream(str2).filter(i&nbsp;->&nbsp;!=null).toArray(String[]::new);&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(String&nbsp;string&nbsp;:&nbsp;strArr)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(string); &nbsp;&nbsp;&nbsp;&nbsp;}在引用类型的情况下,泛型方法从类中被调用并产生,现在接口有两个重载。如果我们使用streamArraysStream<T>StreamtoArray()toArray()然后屈服,在这种情况下我们需要种姓。Object[]toArray(IntFunction<A[]> generator)然后给我们,其中是任何引用类型。A[]A请参阅下面的示例package&nbsp;test;import&nbsp;java.util.Arrays;public&nbsp;class&nbsp;EliminateZeroFromIntArray&nbsp;{public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;int[]&nbsp;in1&nbsp;=&nbsp;{0,&nbsp;0,&nbsp;2,&nbsp;0,&nbsp;3};&nbsp;&nbsp;&nbsp;&nbsp;int[]&nbsp;in2&nbsp;=&nbsp;Arrays.stream(in1).filter(x&nbsp;->&nbsp;x&nbsp;!=&nbsp;0).toArray();&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;:&nbsp;in2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;str&nbsp;=&nbsp;{"a","b",null,"c"}; &nbsp;&nbsp;&nbsp;&nbsp;Object[]&nbsp;array&nbsp;=&nbsp;Arrays.stream(str).filter(i&nbsp;->&nbsp;i&nbsp;!=null).toArray();&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Object&nbsp;object&nbsp;:&nbsp;array)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println((String)object); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;str2&nbsp;=&nbsp;{"a","b",null,"c"}; &nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;strArr&nbsp;=&nbsp;Arrays.stream(str2).filter(i&nbsp;->&nbsp;i&nbsp;!=null).toArray(String[]::new);&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(String&nbsp;string&nbsp;:&nbsp;strArr)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(string); &nbsp;&nbsp;&nbsp;&nbsp;} }}

青春有我

这可能不是你会发现的最好的版本,但是:private int[] removeZeros(int[] array, int toRemove)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; int arrayLength = array.length;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i] == toRemove)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int[] newArray = new int[arrayLength - count];&nbsp; &nbsp; &nbsp; &nbsp; int j = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i] != toRemove)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newArray[j] = array[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return newArray;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java