Why do we keep coming back to arrays in Java?

The use of arrays in Java are quite interesting because you can use a randomGenerator feature in Java to use an array where we initialize a string variable fact to interact with our facts array with interesting factoids about the Java language.

Arrays are powerful features of the Java language because you can randomize the output of the information in the array and initialize strings based on the .length of the array and specific order of statements or “factoids” in the array.

In this case, this simple facts about the Java language:

String[] facts =

// [] after string sets up array and any data in {} pertains to array

{
“Java was designed in 1995 by Sun Microsystems”,
“Nearly 9 out of every 10 computers in the US run Java”,
“Oracle acquired Sun Microsystems in 2010 and oversees all great things relating to Java”,
“Java is ranked #2 in popularity behind C++ which is ranked #1”.
};

// initialize string

String fact = “”;
// Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(facts.length);

fact = facts[randomNumber];

Leave a Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s