Advertisement
LoganBlackisle

arrays

Jun 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package prep_1_arrays;
  2.  
  3. import java.util.Collection;
  4.  
  5. public class Basic {
  6. // declaring arrays
  7. int intArray1[]; // arrays can be made both ways
  8. int[] intArray2;
  9.  
  10. byte byteArray[];
  11. short shortsArray[];
  12. boolean booleanArray[];
  13. long longArray[]; // only basic data types
  14. float floatArray[];
  15. double doubleArray[];
  16. char charArray[];
  17.  
  18. // 2D array
  19. int[][] int2Array = new int[10][20];
  20.  
  21. // 3D array
  22. int[][][] int3Array = new int[10][20][10];
  23.  
  24. Object[] ao; // array of objects
  25.  
  26. @SuppressWarnings("rawtypes")
  27. Collection[] ca;
  28.  
  29. // declaring array AND allocating memory to arrays
  30. int[] intArray3 = new int[20];
  31.  
  32. // or
  33. int[] intArray4 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  34. {
  35. // accessing the elements of the specified array
  36. for (int i = 0; i < intArray4.length; i++) {
  37. System.out.println("Element at index " + i + " : " + intArray4[i]);
  38. }
  39. }
  40.  
  41. public static void main(String[] args) {
  42. // declares an Array of integers.
  43. int[] arr;
  44.  
  45. // allocating memory for 5 integers.
  46. arr = new int[5];
  47.  
  48. // initialize the first elements of the array
  49. arr[0] = 10;
  50.  
  51. // initialize the second elements of the array
  52. arr[1] = 20;
  53.  
  54. // so on...
  55. arr[2] = 30;
  56. arr[3] = 40;
  57. arr[4] = 50;
  58.  
  59. // accessing the elements of the specified array
  60. for (int i = 0; i < arr.length; i++) {
  61. System.out.println("Element at index " + i + " : " + arr[i]);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement