Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sandbox.test01;
- import java.util.Scanner;
- public class Main {
- public static void main(String args [])
- {
- //First 1 dimension array
- int a [] = new int [10]; //allocate memory (capacity is 10)
- for(int i = 0; i < a.length; i++)
- {
- a[i] = i; //Initialize the array with values 0 - 9
- System.out.println("A[" + i + "] : " + a[i]);
- }
- //Second 1 dimension array
- int b [] = new int [5]; //allocate memory (capacity is 5)
- for(int i = 0; i < b.length; i++)
- {
- b[i] = (i * 10); //Initialize the array with values 0-40
- System.out.println("B[" + i + "] : " + b[i]);
- }
- //First 2 dimension array
- int z [] [] = new int[10][]; //allocate memory for 10 arrays but no allocation for the arrays within
- //Assign array[0] to be equal to a
- z[0] = a;
- //Print length of Z
- System.out.println(z[0].length);
- //Print contents of z[0]
- for(int i=0; i < z[0].length; i++)
- {
- System.out.println("Z[" + i + "] : " + z[0][i]);
- }
- //Assign array[0] to be equal to b
- z[0] = b;
- //Print length of Z
- System.out.println(z[0].length);
- //Print contents of z[0]
- for(int i=0; i < z[0].length; i++)
- {
- System.out.println("Z[" + i + "] : " + z[0][i]);
- }
- return;
- }
- }
Add Comment
Please, Sign In to add comment