I'm having some trouble removing null values from a 2D array. I'm still a beginner with programming. I looked for solutions on the web, but I didn't find anything useful.
我在从2D数组中删除空值时遇到一些麻烦。我还是编程的初学者。我在网上寻找解决方案,但我找不到任何有用的东西。
This is and exercise at the university, so changed the name of the array just to "array", same for its objects. This is what I have:
这是在大学里练习,所以将数组的名称改为“数组”,对于它的对象也是如此。这就是我所拥有的:
import java.util.ArrayList;
public class Compact
{
public static void Compact(object[][] array)
{
ArrayList<object> list = new ArrayList<object>();
for(int i=0; i<array.length; i++){
for(int j=0; j < array[i].length; j++){
if(array[i][j] != null){
list.add(array[i][j]);
}
}
}
array = list.toArray($not sure what to typ here$);
}
}
I based this on a solution I found for 1D arrays, but the problem is the list is 1D, so how do I get the structure back of the 2D array? The "new" array has to be smaller, without the null values.
我的解决方案是基于我找到的1D阵列的解决方案,但问题是列表是1D,那么如何获得2D阵列的结构呢? “new”数组必须更小,没有空值。
I thought of making a list for array[i] and one for array[i][j], but then how do I merge them into 1 2D array again?
我想为array [i]创建一个列表,为array [i] [j]创建一个列表,但是如何将它们再次合并为1个2D数组呢?
All help is much appreciated!
非常感谢所有帮助!
========================================
Edit: this is the solution, tnx everyone:
编辑:这是解决方案,tnx大家:
public void compact(Student[][] registrations)
{
for(int i=0; i < registrations.length; i++){
ArrayList<Student> list = new ArrayList<Student>(); // creates a list to store the elements != null
for(int j = 0; j < registrations[i].length; j++){
if(registrations[i][j] != null){
list.add(registrations[i][j]); // elements != null will be added to the list.
}
}
registrations[i] = list.toArray(new Student[list.size()]); // all elements from list to an array.
}
}
3 个解决方案
#1
2
The corresponding redimensionable structure to an Object[][]
is a List<List<Object>>
.
Object [] []的相应可重构结构是List
>。
Also, note that classes in Java always start with an uppercase, by convention. It should thus be Object
, and not object
.
另请注意,按照惯例,Java中的类始终以大写开头。因此它应该是Object,而不是object。
A 2D array is not really a 2D array. It's an array of arrays. There is thus one outer array, and several inner arrays. If you just want to remove nulls from the inner arrays, you can just use one temporary list to hold the elements of the current inner array during the iteration.
2D阵列实际上不是2D阵列。这是一个数组数组。因此有一个外部阵列和几个内部阵列。如果您只想从内部数组中删除空值,则可以在迭代期间使用一个临时列表来保存当前内部数组的元素。
for(int i = 0; i < array.length; i++) {
Object[] inner = array[i];
List<Object> list = new ArrayList<Object>(inner.length);
for(int j = 0; j < inner.length; j++){
if(inner[j] != null){
list.add(inner[j]);
}
}
array[i] = list.toArray(new Object[list.size()]);
}
If the outer array contains null inner arrays, and you also want to remove these, then you shoud use a List<Object[]>
:
如果外部数组包含null内部数组,并且您还想删除它们,那么您应该使用List
List<Object[]> outerList = new ArrayList<Object[]>(array.length);
for(int i = 0; i < array.length; i++) {
Object[] inner = array[i];
if (inner != null) {
List<Object> list = new ArrayList<Object>(inner.length);
for(int j=0; j < inner.length; j++){
if(inner[j] != null){
list.add(inner[j]);
}
}
outerList.add(list.toArray(new Object[list.size()]));
}
}
array = outerList.toArray(new Object[outerList.size()][]);
#2
1
Just tweak your logic, Instead of making toArray at the end make it at the end of first array. Here is the code,s1 have not null values at the end
只需调整你的逻辑,而不是在最后使toArray成为第一个数组的末尾。这是代码,s1最后没有空值
String[][] s = new String[][] { { "00", null, null },
{ "10", "11", null }, { "20", "21", "22" } };
String[][] s1 = new String[s.length][];
int k = 0;
for (int i = 0; i < s.length; i++) {
ArrayList<Object> list = new ArrayList<Object>();
for (int j = 0; j < s[i].length; j++) {
if (s[i][j] != null) {
list.add(s[i][j]);
}
}
s1[k++] = list.toArray(new String[list.size()]);
}
#3
0
For Each Row in the 2D array you can store the not null values in a list. So you will have a list of list containing not null values from the 2D array. Now convert it back to a 2D array.
对于2D数组中的每一行,您可以将非空值存储在列表中。因此,您将获得一个列表,其中包含来自2D数组的非空值。现在将其转换回2D数组。
List<List<Integer> list = new ...
for(int i=0; i<array.length; i++){
List<Integer> templist = new ...
for(int j=0; j < array[i].length; j++){
if(array[i][j] != null){
templist .add(array[i][j]);
}
}
if(!templist.isEmpty()){
list.add(templist);
}
}
..
//convert back to 2D array
int[][] arr2d = new int[list.length][];
for(List<Integer>:list){
... poulate the array accordingly
}
#1
2
The corresponding redimensionable structure to an Object[][]
is a List<List<Object>>
.
Object [] []的相应可重构结构是List
>。
Also, note that classes in Java always start with an uppercase, by convention. It should thus be Object
, and not object
.
另请注意,按照惯例,Java中的类始终以大写开头。因此它应该是Object,而不是object。
A 2D array is not really a 2D array. It's an array of arrays. There is thus one outer array, and several inner arrays. If you just want to remove nulls from the inner arrays, you can just use one temporary list to hold the elements of the current inner array during the iteration.
2D阵列实际上不是2D阵列。这是一个数组数组。因此有一个外部阵列和几个内部阵列。如果您只想从内部数组中删除空值,则可以在迭代期间使用一个临时列表来保存当前内部数组的元素。
for(int i = 0; i < array.length; i++) {
Object[] inner = array[i];
List<Object> list = new ArrayList<Object>(inner.length);
for(int j = 0; j < inner.length; j++){
if(inner[j] != null){
list.add(inner[j]);
}
}
array[i] = list.toArray(new Object[list.size()]);
}
If the outer array contains null inner arrays, and you also want to remove these, then you shoud use a List<Object[]>
:
如果外部数组包含null内部数组,并且您还想删除它们,那么您应该使用List
List<Object[]> outerList = new ArrayList<Object[]>(array.length);
for(int i = 0; i < array.length; i++) {
Object[] inner = array[i];
if (inner != null) {
List<Object> list = new ArrayList<Object>(inner.length);
for(int j=0; j < inner.length; j++){
if(inner[j] != null){
list.add(inner[j]);
}
}
outerList.add(list.toArray(new Object[list.size()]));
}
}
array = outerList.toArray(new Object[outerList.size()][]);
#2
1
Just tweak your logic, Instead of making toArray at the end make it at the end of first array. Here is the code,s1 have not null values at the end
只需调整你的逻辑,而不是在最后使toArray成为第一个数组的末尾。这是代码,s1最后没有空值
String[][] s = new String[][] { { "00", null, null },
{ "10", "11", null }, { "20", "21", "22" } };
String[][] s1 = new String[s.length][];
int k = 0;
for (int i = 0; i < s.length; i++) {
ArrayList<Object> list = new ArrayList<Object>();
for (int j = 0; j < s[i].length; j++) {
if (s[i][j] != null) {
list.add(s[i][j]);
}
}
s1[k++] = list.toArray(new String[list.size()]);
}
#3
0
For Each Row in the 2D array you can store the not null values in a list. So you will have a list of list containing not null values from the 2D array. Now convert it back to a 2D array.
对于2D数组中的每一行,您可以将非空值存储在列表中。因此,您将获得一个列表,其中包含来自2D数组的非空值。现在将其转换回2D数组。
List<List<Integer> list = new ...
for(int i=0; i<array.length; i++){
List<Integer> templist = new ...
for(int j=0; j < array[i].length; j++){
if(array[i][j] != null){
templist .add(array[i][j]);
}
}
if(!templist.isEmpty()){
list.add(templist);
}
}
..
//convert back to 2D array
int[][] arr2d = new int[list.length][];
for(List<Integer>:list){
... poulate the array accordingly
}