1

So I need to print out an array of integers. The problem is that when user enters the numbers to be processed and sorted, I do not know how many numbers there will be entered by the user. The only rule to that is that user can enter only less than 10000 numbers.

So I made an array which can hold 10000 numbers but if user enters less than 10000 numbers into the array then Array.toString() function prints out everything, even the empty spaces.

Is there any way to bypass that or are there any other methods for outputting an array in one line that would format it the output to look like this: [1, 2, 3, 4, 5, 6]

Thanks a lot!

2
  • 1
    What about using an ArrayList<T>? Commented Oct 3, 2014 at 13:43
  • 2
    Use a list rather than an array. Then convert it back to an array if you really need to (but you shouldn't). You can iterate across the list easily enough and it has a dynamic length. Commented Oct 3, 2014 at 13:44

3 Answers 3

4

It would be much easier to store the user's input in an ArrayList, and then just print myArryList.toString().

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

1 Comment

@Artur: Well for most applications, arrays are too low level (especially because Java has some troubles with primitive types). Only if your application should get the maximum out of a CPU, one should use arrays...
2

An ArrayList<T> (optionally <T> for generics). Is an array that dynamically adds more memory if more input comes available. The amortized cost of adding an element to such list is O(1), but it offers a convenient way to process input.

To answer your question, as @Mureinik already answered you then can use ArrayList.toString() to convert the list to a textual representation.

To answer your real question, you can do the following:

public static<T> String toStringArrayNonNulls (T[] data) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    int n = data.length;
    int i = 0;
    for(; i < n; i++) {
        if(data[i] != null) {
            sb.append(data[i].toString());
            break;
        }
    }
    for(; i < n; i++) {
        if(data[i] != null) {
            sb.append(",");
            sb.append(data[i].toString());
        }
    }
    sb.append("]");
    return sb.toString();
}

And call that method with any type of array you want.

Examples

String[] names = new String[] {"Alice",null,"Charly","David",null,null};
System.out.println(toStringArrayNonNulls(names));
Integer[] primes = new Integer[] {2,3,null,null,11};
System.out.println(toStringArrayNonNulls(primes));
Object[] namesAndPrimes = new Integer[] {"Alice",2,null,3,null,"Charly",null,11};
System.out.println(toStringArrayNonNulls(namesAndPrimes));

2 Comments

Thanks! Time to get used to ArrayLists I guess.
Just tried your toStringArrayNonNulls method and it still prints whole array and adds one extra number in there. Tried to figure out why but no clue though. For example if I have 3 arrays of the same size, i add their first element to together and divide by 3 and do this to all elements and store it into the final array of integers to be printed out. If I set all arrays to be "Integers" it thorws a NullPointerException but if only the final array then it prints out every element of the array. No clue what is wrong.
2

If you want a dynamic array in Java, the best way is to learn how to use lists :

List<Integer> integers = new ArrayList<Integer>();

It prints only as many numbers as you put into it.

If you dont want to rewrite your program, this is also solution :

    Integer[]x = new Integer[50];
    List<Integer> integers = new ArrayList<Integer>();        
    integers = new ArrayList<Integer>(Arrays.asList(x));

2 Comments

Solution is good, just want to add that a LinkedList might be better if you only have insert and loop operations when storing 10000 integers, performance wise.
@Penterekakaak: Well the amortized cost of adding one element remains O(1), so speaking in terms of big oh, there is no difference. I agree there are some situations where it matters.

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.