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>();
source share