Guest User

Untitled

a guest
Jan 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. // code is stored in containers called "classes" in java
  2. public class Driver {
  3.  
  4. // this part of code is automatically executed when the
  5. // program is run because it's called "main"
  6. public static void main(String[] args) throws Exception {
  7.  
  8. // below we create our arrays
  9. int[] myArray1 = new int[10]; // this creates a new array "myArray" with 10 blank elements of type int
  10. int[] myArray2; // this creates a blank array without a size (we'll give it a size later)
  11.  
  12. // this creates an array of size 6,
  13. // (we provided 6 integers, so java will automatically give it a size of 6).
  14. // because we manually wrote these 6 numbers in our code,
  15. // java will automatically put these values into our array.
  16. int[] myArray3 = {38,2,52,65,12,44};
  17.  
  18. // because myArray1 is empty, we need to give it some values
  19. myArray1[0] = 10; // we can manually give the array different values like this
  20. myArray1[1] = 25;
  21. myArray1[2] = 13;
  22. myArray1[3] = 98;
  23. myArray1[4] = 74;
  24. myArray1[5] = 38;
  25. myArray1[6] = 64;
  26. myArray1[7] = 37;
  27. myArray1[8] = 56;
  28. myArray1[9] = 13; // we stop here because we started counting at zero
  29.  
  30. // myArray2 doesn't even have a size, so lets give it one...
  31. myArray2 = new int[3]; // it now has a size of 3 and doesn't contain any data
  32. myArray2[0] = 2;
  33. myArray2[1] = 7;
  34. myArray2[2] = 16;
  35.  
  36. // now let's print out our data
  37. for (int i=0; i<10; i++)
  38. System.out.println(myArray1[i]);
  39. System.out.println(); // this prints a blank space so we can separate the output
  40.  
  41. for (int i=0; i<3; i++)
  42. System.out.println(myArray2[i]);
  43. System.out.println();
  44.  
  45. for (int x=0; x<myArray3.length; x++) // this is an alternative, fancier way of writing it
  46. System.out.print(myArray3[x] + " ");
  47.  
  48. /*
  49. Here's what we see in our output window after running this program:
  50.  
  51. 10
  52. 25
  53. 13
  54. 98
  55. 74
  56. 38
  57. 64
  58. 37
  59. 56
  60. 13
  61.  
  62. 2
  63. 7
  64. 16
  65.  
  66. 38 2 52 65 12 44
  67. */
  68. }
  69.  
  70. }
Add Comment
Please, Sign In to add comment