Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7069055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:23:57+00:00 2026-05-28T05:23:57+00:00

I’m having some trouble removing null values from a 2D array. I’m still a

  • 0

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.

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.

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?

All help is much appreciated!

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

Edit: this is the solution, tnx everyone:

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.
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:23:58+00:00Added an answer on May 28, 2026 at 5:23 am

    The corresponding redimensionable structure to an Object[][] is a List<List<Object>>.

    Also, note that classes in Java always start with an uppercase, by convention. It should thus be Object, and not 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.

    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[]>:

    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()][]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.