Arrays are setup with like a coulomb or row, they can hold different type of value of values.
So how do you tell java to create an array? the syntax is simple but before that what type of array would you like to create? what is going to be storing? (integers, strings, boolean values, etc) once you have the the sytnax goes as follow [decelration][brackets][name] = [new] [int] [Number of array];
int [] iArray = new int 10;
Now that we know what an array what we can unveil the secrete of java,
have you ever wondered what this is?
public static void main (String [] args)
the args part about the piece of code simple says that arrays can be passed to this main method. Knowing this much arrays can be made easily but don’t get carried away, the array always set a default value for the type of array, so when a array is created the values get initialized for example
int = 0
double = 0.0
so on..
/*************************** Code Examples ******************************/
import java.util.Random; public class ArrayDemo { public void runDemo () { int [] iArray; // decalre the bran new array iArray = new int [10]; Random random = new Random (); // new random generator for (int i=0; i < iArray.length; i++) // get an assigment to the variables { iArray[i] = random.nextInt(100) + 1; //sets i to the array ouptuing the numbers from 0-9 System.out.printf("%2d %3d\n", i, iArray[i]); // gets executed from 0-9 } } }
/******************* Run Method ***************************/
public class ArrayDemoTest { public static void main(String [] args) { ArrayDemo arrayDemo = new ArrayDemo(); arrayDemo.runDemo(); }// end of main } // end of arraydemotest class
I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More