This is the code for my project.
public class PhraseOMatic {
public static void main(String[] args) {
String[] wordListOne = {"24/7", "multi-Tier", "30,000 foot", "B-to-B",
"win-win", "front-end", "web-based", "pervasive", "smart", "sixsigma”,"};
String[] wordListTwo = {"empowered", "sticky", "value-added", "oriented",
"centric", "distributed", "clustered", "branded", "outsidethebox",
"positioned", "networked", "focused", "leveraged", "aligned", "targeted", "shared”"};
String[] wordListThree = {"process", "tipping-point", "solution",
"architecture", "core competency”",
"portal", "space", "vision", "paradigm", "mission"};
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);
String phrase = wordListOne[rand1] " " wordListTwo[rand2] " "
wordListThree[rand3];
System.out.println("What we need is a " phrase);
}
}
It is mostly taken from one of my lecturers. But I had to change a couple of things to get it to actually run.
My question however is, when do I have to import java.util.Arrays? It runs without it being imported even though I'm using arrays. So, why is this?
CodePudding user response:
An array is not the same as java.util.Arrays
.
Java language has a builtin array type that you can construct with expressions like String[]
, int[]
, etc. You don't have to declare anything to use them.
java.util.Arrays
is not the type of this kind of object. It is a tool type that contains a lot of methods to be used on arrays: sort them, convert them to and another collection, etc. If you want to use such a class, you need to either use the Fully Qualified Name e.g. java.util.Arrays
or declare it as import java.util.Arrays
in order to use just Arrays
instead of java.util.Arrays
every time you need it in a class. The latter approach also makes your code look cleaner and save some typing.
CodePudding user response:
You aren't using any of the method from https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html. That's why you don't need it.
[]
is part of the language like ;
or %
, it's defined here https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3.1
In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
CodePudding user response:
You only need to import java.util.Arrays
when you are explicitly using one of the methods that this class provides. All it does is provide useful methods for handling arrays. If you don't call any of them, then you also don't need to import it.
It helps to understand that an import
in Java is nothing more than a way to refer to a class by its simple name (i.e. allows you to write Arrays.sort(someArray)
instead of having to type java.util.Arrays.sort(someArray)
.
CodePudding user response:
java.util.Arrays
is a class. You need to import this class when you use this class. Same as with all other classes (except classes from the java.lang
package, which are imported by default). In your code you do not make use of the java.util.Arrays
class, so you don`t need to import it.