Crenox

Arrays in Java

Apr 29th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package com.java.main;
  2.  
  3. public class Sam
  4. {
  5. public static void main(String args[])
  6. {
  7. int[] ages; // declaration
  8.  
  9. ages = new int[100]; // creation
  10.  
  11. ages[0] = 16;
  12. System.out.println(ages[0] + "\n");
  13. ages[1] = 18;
  14.  
  15. ages[0]++;
  16. System.out.println(ages[0] + "\n");
  17.  
  18. for(int x = 0; x < 100; x++)
  19. {
  20. System.out.println(ages[x] + "\n");
  21. }
  22.  
  23.  
  24. int[] ages1 = new int[5];
  25.  
  26.  
  27. int[] ages2 = {16, 18, 15, 16, 17}; // initializer list
  28.  
  29.  
  30. int[] ages3 = new int[]{16, 18, 15, 16, 17};
  31.  
  32.  
  33.  
  34.  
  35. String[] names = new String[100];
  36. names[0] = "Sammy";
  37. names[1] = "Brendan";
  38. for(int i = 0; i < 100; i++)
  39. {
  40. System.out.println("Name is: " + names[i] + " | Age is: " + ages[i] + "\n");
  41. }
  42. }
  43. }
  44. /* Ways to Declare an Array
  45. int[] myIntArray = new int[3];
  46. int[] myIntArray = {1,2,3};
  47. int[] myIntArray = new int[]{1,2,3};
  48. */
Advertisement
Add Comment
Please, Sign In to add comment