Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // code is stored in containers called "classes" in java
- public class Driver {
- // this part of code is automatically executed when the
- // program is run because it's called "main"
- public static void main(String[] args) throws Exception {
- // below we create our arrays
- int[] myArray1 = new int[10]; // this creates a new array "myArray" with 10 blank elements of type int
- int[] myArray2; // this creates a blank array without a size (we'll give it a size later)
- // this creates an array of size 6,
- // (we provided 6 integers, so java will automatically give it a size of 6).
- // because we manually wrote these 6 numbers in our code,
- // java will automatically put these values into our array.
- int[] myArray3 = {38,2,52,65,12,44};
- // because myArray1 is empty, we need to give it some values
- myArray1[0] = 10; // we can manually give the array different values like this
- myArray1[1] = 25;
- myArray1[2] = 13;
- myArray1[3] = 98;
- myArray1[4] = 74;
- myArray1[5] = 38;
- myArray1[6] = 64;
- myArray1[7] = 37;
- myArray1[8] = 56;
- myArray1[9] = 13; // we stop here because we started counting at zero
- // myArray2 doesn't even have a size, so lets give it one...
- myArray2 = new int[3]; // it now has a size of 3 and doesn't contain any data
- myArray2[0] = 2;
- myArray2[1] = 7;
- myArray2[2] = 16;
- // now let's print out our data
- for (int i=0; i<10; i++)
- System.out.println(myArray1[i]);
- System.out.println(); // this prints a blank space so we can separate the output
- for (int i=0; i<3; i++)
- System.out.println(myArray2[i]);
- System.out.println();
- for (int x=0; x<myArray3.length; x++) // this is an alternative, fancier way of writing it
- System.out.print(myArray3[x] + " ");
- /*
- Here's what we see in our output window after running this program:
- 10
- 25
- 13
- 98
- 74
- 38
- 64
- 37
- 56
- 13
- 2
- 7
- 16
- 38 2 52 65 12 44
- */
- }
- }
Add Comment
Please, Sign In to add comment