0

I'm reading from an XML file to populate some data structures and I run into this sort of problem when I inspect my final structure:

arrayName
 [0] = null
 [1] = input
 [2] = null
 [3] = input

etc

input is what I want, and the type is my own class.

I'm used to C# so I'd use LINQ to get rid of them normally, idea for doing something like this in Java?

I'm going to look at what's wrong with the code that's making it happen but for now I need a quick solution.

Ideas?

EDIT:

I found the issue, I create an array of size doc.getChildNodes().getLength(), and when I'm setting elements in the array (while looping through), I check if

 getNodeType() == Node.ELEMENT_NODE) 

And it doesn't work half the time. Problem is, I initialise the array based on the size, so half the array gets filled.

Ideas?

3
  • You don't remove null. null kind of means removed or lack of. Commented Dec 29, 2013 at 0:27
  • 2
    Show us your full code please. Commented Dec 29, 2013 at 0:29
  • You can't "remove" array elements. Do you mean "compact" the array so there's no nulls but the order of non-null references remains the same? Commented Dec 29, 2013 at 0:35

4 Answers 4

1

Arrays are immutable in Java (not their contents, the array itself). There is no such thing as a dynamic sized array or changing of the length. So you would iterate, count, create a new array, copy... or use an appropriate datastructure in the first place, maybe even one that capsules the creation of new arrays and offers manipulation methods like ArrayList.

Something like LINQ does not exist yet, you need some explicit object that capsules the manipulation or filtering.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're not having excessive amounts of data, you could just blend it through a few collections.

public class Main {
    public static void main(String[] args) {
        String[] arr = {"haha", "hoho", null, "hihi", null };
        System.out.println(Arrays.toString(arr));

        Set<String> set = new HashSet<>(Arrays.asList(arr));
        set.remove(null);
        arr = new String[set.size()];
        arr = set.toArray(arr);

        System.out.println(Arrays.toString(arr));
    }
}

Output:

[haha, hoho, null, hihi, null]
[hoho, haha, hihi]

Keep in mind that this first allocates the original array, then creates a list from that array, then creates a hashset from that list and eventually puts everything back in the original array.

If you're having a very large amount of data it might constipate you a little but otherwise it's very easy to read and really just uses built-in features to reach what you want.

1 Comment

Thanks, might be a bit too inefficient for what I need though, I added why it's happening if you have an idea of how to fix it?
0

Unfortunately, there's nothing like LINQ in Java. The best thing would be probably checking for null beforehand, and only inserting if the element is not null, eg. (assuming your class name is InputClass:

InputClass c = parseFromXml(...);
if (c != null) {
    myList.add(c);
}

Alternatively, you can remove nulls by iterating and copying (I use a list as an intermediary artifact):

InputClass[] removeNulls(InputClass[] original) {
    List<InputClass> nonNulls = new ArrayList<InputClass>();
    for (InputClass i : original) {
        if (i != null) {
            nonNulls.add(i);
        }
    }

    return nonNulls.toArray(new InputClass[0]);
}

You can also use generics and make your method <T> removeNulls(T[] original) instead.

Comments

0

It sounds like you want a method to give you a new Array with the null values removed; perhaps something like this -

public static <T> T[] removeNulls(T[] in) {
  if (in == null) {
    return in;
  }
  Class<?> cls = null;
  List<T> al = new ArrayList<T>();
  for (T t : in) {
    if (t != null) {
      if (cls == null) {
        cls = t.getClass();
      }
      al.add(t);
    }
  }
  @SuppressWarnings("unchecked")
  T[] out = (T[]) Array.newInstance(cls, al.size());
  return al.toArray(out);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.