When working with arrays you want to think about them like a shelf you can take things in and out of them and see what they store. Arrays can be declared by different types from int to strings although int arrays get an initial value of zero so in this example i create an array of the size of 15, that starts off from the number 0 and moves up to shelf 14 making it size 15, you see we always count starting at zero. I create the header with the following notations to get a nice pretty layout of the data, as follows %s%8s\n, look at the this for modulus definition Entering the for loop structure where it creates a variable of c and sets it to zero, then compares c to the arrays length and iterates threw the loop until it reaches the size of the array and exits, this is controlled looping.
public class Program { public static void main(String args[]) { int [] array; // creates an array array = new int[15]; // sets the value of the array System.out.printf("%s%8s\n", "indexx", "value"); // prints out the Header // sets a value for c and enter the for loop pro the size of the array denoting .length for (int c= 0; c < array.length; c++) { System.out.printf("%5d%8d\n", c, array[c]); } } }
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