how to use Arrays.toString directly in java (without Arrays. before it)

84 views Asked by At

Imagine I make an array e.g. int[] a = new int[5], why I cannot use System.out.print(toString(a))? I imported the method by the way, import static java.util.Arrays.toString; but I get compile error I'm assuming I'm using a wrong import for example I had no issue with Arrays.fill and I can use fill directly , e.g. fill(a,10) works perfectly fine but toString(a) will have compile error

1

There are 1 answers

0
rzwitserloot On

What you want is impossible.

The problem is specifically that all classes inherently extend from Object; you can't opt out of that (even if you write no extends clause at all, such as class Foo {}, then javac compiles this as if you wrote class Foo extends Object{}; you can't tell javac not to do this).

And java.lang.Object defines the method: public String toString() { ...}. Which means your class, like every class in all of java, has a method named toString(). You can't tell javac to un-define a method. It's there. Whether you write it or not.

And static imports are defined as simply not existing if their name clashes with a name your class already has, even if the parameter list cannot match up (the various Arrays.toString method all take at least 1 param, the toString() your own class has, takes zero).

Hence, what you want, is impossible. It's an artefact of the choice of name: Had that Arrays.toString() method been named anything else, you could have used a static import.

Solution 1 - stop using arrays

The primary solution is to stop using arrays - they are low level constructs that should be used only by libraries that offer high level features (i.e. that write the high level features, based on low level features). An example: java.util.ArrayList internally uses an array - that's appropriate use of arrays.

This advice definitely counts very very strongly when you make arrays of non-primitive types (i.e. any java code that uses String[] anywhere is suspect. There are loads of downsides, and effectively zero upsides, to String[] in contrast to a List<String>). Less so when we're talking about arrays of primitive types, such as int[] (there are quite a few good reasons to use an int[] instead of a List<Integer>).

Lists don't need these kludges. Their toString() is fine. Their hashCode() and equals() implementations are fine. They can grow and shrink.

Solution 2 - bridgers

You can of course do the work here. You can for example make this:

public class ArrayTools {
  public static String arrToString(int[] a) {
    return Arrays.toString(a);
  }
}

and then use it:

import static com.foo.hashemi.ArrayTools.*;

void example() {
  int[] x = {1, 2};
  System.out.println(arrToString(x));
}

Solution 3 - lombok extensionmethod

Add lombok to your project, then:

class ArrayTools {
  public static String arrToString(int[] a) {
    return Arrays.toString(a);
  }
}

// and to use

@ExtensionMethod(ArrayTools.class)
class Example {
  void foo() {
    int[] x = {1, 2};
    System.out.println(x.arrToString());
  }
}

For the same reason the static import of Arrays.toString() doesn't work, you can't extension-method Arrays.toString onto int[] and friends - because it clashes with the existing (useless) toString() all objects (and arrays are objects) have.

Solution 4 - set up your tools to make this less annoying

Arrays.toString() isn't that big a deal. Most IDEs can be told to default-include all methods in a certain class in auto-complete dialogs (i.e. assume they are imported by default). For example in eclipse, open your preferences, then navigate to:

Java -> Editor -> Content Assist -> Favorites

And add java.util.Arrays.toString to the list. Now you can simply type toStr anywhere, hit CTRL+SPACE (or whatever keyboard shortcut you set up for autocomplete) and Arrays.toString will be in the list.