1

I am trying to print a determined array in String form withoput using Arrays.toString function. What I have tried is the following but it turns out that method toString cannot be found and I don't know really what I am missing. I have hard times understanding the whole thinking process behind Java, so it would be great if you could explain me in detail what is happeing on each step. Thank a lot in advance!

class RandomIntArray { 
public static String toString(int[] a) {
       int[] array = {1, 2, 3, 4};
       System.out.print("[" + array[0]);
       for (int i = 1; i < array.length; i++) {
       System.out.print(", " + a[i]);
       System.out.println("]");
           }
    }
       public static void main (String[]args){
            System.out.println(toString[i]);
        }
    }
1
  • 1
    You misunderstand between arrays array, a . Also you don't call method toString, you try to access it like an array Commented Nov 22, 2020 at 10:57

1 Answer 1

4

Problems

  • toString[i] is not a method call (toString() is), brackets [] are for array access
  • you misunderstand between arrays array and a
  • you method toString must return a String but you return nothing, and print all inside the method itself

Solution

  • the initial array must be defined in the main, and passed to the method
  • the method toString must build a String and return it
public static String toString(int[] a) {
    StringBuilder sb = new StringBuilder("[" + a[0]);
    for (int i = 1; i < a.length; i++) {
        sb.append(", ").append(a[i]);
    }
    return sb.append("]").toString();
}

public static void main(String[] args) {
    int[] array = {1, 2, 3, 4};
    System.out.println(toString(array));
}

Improvement: handle empty array

It may be better to put all array access into the loop, because in case the array is empty, the previous code will fail at a[0]

public static String toString(int[] a) {
    String join = "";
    StringBuilder sb = new StringBuilder("[");
    for (int val : a) {
        sb.append(join).append(val);
        join = ", ";
    }
    return sb.append("]").toString();
}

Example

System.out.println(toString(new int[]{1, 2, 3, 4})); // [1, 2, 3, 4]
System.out.println(toString(new int[]{}));           // []
Sign up to request clarification or add additional context in comments.

3 Comments

It's worth to add checks 1) for null; 2) for the length of array before including a[0] to print [] for empty array :)
@AlexRudenko I've just changed that, a second ago :)
Thanks a lot for your explanation! I am so sorry for bothering you but I am really having a hard time tying to understand the thinking process of Java: I have one main question regarding your solution: what you do at the beginning with "sb" is create an array that concatenates "[" symbol and the first argument of our given array right? And the for moves along the array until the last element is reached while adding commas. Is that correct?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.