If a normal variable is like a motorcycle, then an array is like a big city bus. It is made specifically to hold many items. Arrays in Java are similar to arrays in any other language. They need to have a type (i.e. int, String, char, double, etc..), and you need to specify how big the array will be so the appropriate amount of memory can be allocated for it. Java arrays, like arrays in most languages, are zero-indexed, meaning the first item in the list has an index of zero. Ok, enough talk, lets code.
For this project I recommend using Netbeans IDE. I explained how to install and setup Netbeans in this post. So if you haven’t read that yet, I would recommend checking it out.
- First off, we will need to create a new project, you can title it whatever you like. Then, once you are inside the project folder, create a new blank Java file.
- Ok, go ahead and hit enter a couple times to give yourself a little room at the top of the page. We will need to create our class first, So type:
- Inside those curly brackets press enter and type “psvm” then hit ‘tab.’ This will create the “main method.” Every Java program has one. It’s where the JVM (Java Virtual Machine) looks to run your code, and it should look like this:
- Now, lets declare some arrays. So, inside the main method, go ahead and type this:
- Lets assign those arrays some memory.
- Cool! Now we just need to actually put some stuff in these arrays. The code below uses a for loop to loop through the array and adds a number to each spot in the array.
- Awesome, that's how you declare, allocate, and initialize an array in Java. The importance of arrays is staggering. I would bet that every single app or program you use makes use of arrays in some way. Lets finish this program off by printing out the grades and determining whether they passed the class or not.
- Here is the full code for those of you who just have a stupid homework question you have to answer and don't have time to read, lol. TLDR:
class ArrayPractice{}
*Here, make sure you replace “ArrayPractice” with the name of your project
class ArrayPractice{
public static void main(String[] args) {
}
}
int[] age;
int grades[];
*Both of these are valid array declarations.
age = new int[10];
grades = new int[10];
*These last two steps can be combined into a single step like this:
int[] age = new int[10];
int[] grades = new int[10];
-For this we will need to import the random package, so we can randomly assign values to the arrays. So at the top of your page type: import java.util.Random;
//This creates a new Random class object
//that we can use to create random numbers
Random rand = new Random();
//loop through to add elements to the array
for (int i = 0; i < grades.length; i++) {
//generate two random numbers
int rand_int1 = rand.nextInt(100);
int rand_int2 = rand.nextInt(100);
//add one to each array, for each loop cycle
grades[i] = rand_int1;
age[i] = rand_int2;
}
for (int i = 0; i < grades.length; i++) {
if(grades[i] >= 90){
System.out.println(grades[i] + " A, Pass");
}else if(grades[i] >= 80){
System.out.println(grades[i] + " B, Pass");
}else if(grades[i] >= 70){
System.out.println(grades[i] + " C, Barely pass");
}else if(grades[i] >= 60){
System.out.println(grades[i] + " D, Sucks to suck");
}else{
System.out.println("What are you even doing??");
}
}
/**
*Java Arrays
*/
import java.util.Random;
public class ArrayPractice {
public static void main(String[] args) {
int[] age = new int[10];
int[] grades = new int[10];
//This creates a new Random class object
//that we can use to create random numbers
Random rand = new Random();
//loop through to add elements to the array
for (int i = 0; i < grades.length; i++) {
//generate two random numbers
int rand_int1 = rand.nextInt(100);
int rand_int2 = rand.nextInt(100);
//add one to each array, for each loop cycle
grades[i] = rand_int1;
age[i] = rand_int2;
}
// //print out student ages
// System.out.println("Student ages:");
// for (int i = 0; i < age.length; i++) {
// System.out.println(age[i]);
// }
//determine passing grades
for (int i = 0; i < grades.length; i++) {
if(grades[i] >= 90){
System.out.println("Grade: " + grades[i] + ", A, Pass");
}else if(grades[i] >= 80){
System.out.println("Grade: " + grades[i] + ", B, Pass");
}else if(grades[i] >= 70){
System.out.println("Grade: " + grades[i] + ", C, Barely pass");
}else if(grades[i] >= 60){
System.out.println("Grade: " + grades[i] + ", D, Sucks to suck");
}else{
System.out.println("Grade: " + grades[i] + ", What are you even doing??");
}
}
}
}
Should look something like this: