0

I have 3 generic arrays which all have a pre-determined size of 25. For example, if its only 5 actual values in the array, it will display those 5, than the next 20 as null. I dont want to display any null values. Do I edit something in the toString? My toString is

public String toString(){
  return Arrays.toString(container);
}

Container being an array object created from a constructor function. Here is what my output looks like..

Picture of output

5
  • Please import the result in your post, don't use external link. Commented Feb 13, 2019 at 15:29
  • 1
    I don't understand how taking and uploading a photo of your screen seems to be easier than copy/pasting that one line of text into the question. Commented Feb 13, 2019 at 15:31
  • stackoverflow.com/questions/26353491/…? Commented Feb 13, 2019 at 15:31
  • 1
    It's unclear what a "generic array" is. Do you mean an array whose element type is given by a type variable? Or perhaps simply an explicit Object[]? Commented Feb 13, 2019 at 15:33
  • Sounds like a poor datamodel to me. Either use ArrayList instead of array or keep a top pointer to the first vacant slot so you know which part of the array to print. Then there are a number of options for printing. Commented Feb 13, 2019 at 15:34

4 Answers 4

2

The Arrays.toString() methods all produce a representation of the whole array. If that's not what you want, then you need to choose a different approach. For example, you might do something like this:

String result = Arrays.stream(container)
        .filter(Objects::nonNull)
        .map(Objects::toString)
        .collect(Collectors.joining(",", "[", "]"));
Sign up to request clarification or add additional context in comments.

1 Comment

I don’t really see the hacky part about it. At least not compared to the other answers.
2

In the following, I suppose your container is something like Something []container.

Pre-java 8 using StringBuilder:

public String toString(){
    StringBuilder s = new StringBuilder();
    for (Something i : container) if (i!=null) s.append(i.toString()+",");
    return s.toString();
}

Post-java 8 using streams:

public String toString(){
    return Arrays.stream(container)
                 .filter(Objects::nonNull)
                 .map(Object::toString)
                 .collect(Collectors.joining(","));
}

1 Comment

Thank you, I will try your solutions when I get to my laptop later on.
1

Assuming you use java8 or later:

Arrays.toString(Arrays.stream(container).filter(i->i!=null).collect(Collectors.toList()));

Comments

1

Another solution with java8 stream:

MyType[] array = Arrays.stream(originalArray)
                       .filter(Objects::nonNull)
                       .toArray(MyType[]::new);
    
System.out.println(Arrays.toString(array));

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.