How to remove null values ​​from a 2D array?

I'm having trouble deleting null values ​​from a 2D array. I am still new to programming. I searched for solutions on the Internet, but I did not find anything useful.

This is an exercise at the university, so it changed the name of the array only to "array", the 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 the solution I found for 1D arrays, but the problem is that the list is 1D, so how do I get the structure back from the 2D array? The "new" array must be smaller, without null values.

I was thinking of creating a list for array [i] and one for array [i] [j], but how can I merge them again into 1 2D array?

All help is much appreciated!

==========================================

Edit: this is the solution, tnx every:

 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. } } 
+4
source share
3 answers

The corresponding custom structure for Object[][] is List<List<Object>> .

Also note that classes in Java always start with capital letters, conditionally. It should be an Object , not an Object .

A 2D array is not a 2D array. This is an array of arrays. Thus, there is one external array and several internal arrays. If you just want to remove zeros from inner arrays, you can simply use one temporary list to hold the elements of the current inner array during iteration.

 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 zero inner arrays and you also want to delete them, then you use List<Object[]> :

 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()][]); 
+1
source

Just adjust your logic. Instead of making toArray at the end, do it at the end of the first array. Here is the code, s1 do not have zero values ​​at the end

  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()]); } 
+1
source

For each row in a 2D array, you can store non-zero values ​​in a list. This way you will have a list list containing non-null values ​​from a 2D array. Now translate it back to the 2D array.

 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 } 
0
source

Source: https://habr.com/ru/post/1391607/


All Articles