Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prep_1_arrays;
- import java.util.Collection;
- public class Basic {
- // declaring arrays
- int intArray1[]; // arrays can be made both ways
- int[] intArray2;
- byte byteArray[];
- short shortsArray[];
- boolean booleanArray[];
- long longArray[]; // only basic data types
- float floatArray[];
- double doubleArray[];
- char charArray[];
- // 2D array
- int[][] int2Array = new int[10][20];
- // 3D array
- int[][][] int3Array = new int[10][20][10];
- Object[] ao; // array of objects
- @SuppressWarnings("rawtypes")
- Collection[] ca;
- // declaring array AND allocating memory to arrays
- int[] intArray3 = new int[20];
- // or
- int[] intArray4 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- {
- // accessing the elements of the specified array
- for (int i = 0; i < intArray4.length; i++) {
- System.out.println("Element at index " + i + " : " + intArray4[i]);
- }
- }
- public static void main(String[] args) {
- // declares an Array of integers.
- int[] arr;
- // allocating memory for 5 integers.
- arr = new int[5];
- // initialize the first elements of the array
- arr[0] = 10;
- // initialize the second elements of the array
- arr[1] = 20;
- // so on...
- arr[2] = 30;
- arr[3] = 40;
- arr[4] = 50;
- // accessing the elements of the specified array
- for (int i = 0; i < arr.length; i++) {
- System.out.println("Element at index " + i + " : " + arr[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement