00:00
/* C02 Array Code */
public class C02 {
public static void main(String[] args) {
int [] b = new int[-3]; /* java.lang.NegativeArraySizeException: -3 */
}
}
// Exception in thread "main" java.lang.NegativeArraySizeException: -3
// at C02.main(C02.java:5)
/* C_06 Reteriving the element form one d array */
public class C_06 {
public static void main(String[] args) {
System.out.println("** Reteriving the element form one d array **");
int[] i ;
i = new int [4];
i[0] = 10;
i[1] = 20;
i[2] = 30;
i[3] = 40;
for(int j=0;j<i.length;j++){
System.out.println(i[j]);
}
}
}
// ** Reteriving the element form one d array **
// 10
// 20
// 30
// 40
/* C_07, Check for 0 array size */
public class C_07 {
public static void main(String[] args) {
int [] a = new int[0]; /* no issue */
}
}
/* C_08, for printing using for, foreach annd lenght function*/
public class C_08 {
public static void main(String[] args) {
int[] a = {10,20,30};
System.out.print("for = ");
for(int i=0;i<=2;i++){
System.out.print(a[i]+" ");
}
System.out.print("\nfor each = ");
for( int j:a){
System.out.print(j+" ");
}
System.out.print("\nlenght = ");
for( int k=0;k<a.length;k++){
System.out.print(a[k]+" ");
}
}
}
// for = 10 20 30
// for each = 10 20 30
// lenght = 10 20 30 %
/* C_13 lenght for 1dA */
public class C_13 {
public static void main(String[] args) {
/* length for 1dA */
int[] a = new int[3];
System.out.println("a = "+a.length);
}
}
// a = 3
/* C_14 lenght 2dA */
public class C_14 {
public static void main(String[] args) {
int[][] b = new int[3][2]; //2dA lenght
System.out.println("b = "+b.length); //
System.out.println("b[0] = "+b[0].length); // Index 0 Pointing 2 Length Array
System.out.println("b[1] = "+b[1].length); // Index 1 Pointing 2 Length Array
System.out.println("b[2] = "+b[2].length); // Index 2 Pointing 2 Length Array
int[][] c = { {10,20} , {50,60}, {50,60} } ; // @da lenght with element
System.out.println("c[1] = "+c[1].length); // 2
}
}
// b = 3
// b[0] = 2
// b[1] = 2
// b[2] = 2
// c[1] = 2
/* C_15 lenght 2d jagged Array */
public class C_15 {
public static void main(String[] args) {
int[][] d = new int[4][]; // length for 2dA jagged
d[0] = new int[3];
d[1] = new int[4];
d[2] = new int[2];
System.out.println("d = "+d.length);
System.out.println("d[0] = "+d[0].length); // 3
System.out.println("d[1] = "+d[1].length); // 4
System.out.println("d[2] = "+d[2].length); // 2
System.out.println("d[3] = "+d[3].length); // .NullPointerException, coz not defined
}
}
// d = 4
// d[0] = 3
// d[1] = 4
// d[2] = 2
// Exception in thread "main" java.lang.NullPointerException: Cannot read the array
// length because "<local1>[3]" is null at C_15.main(C_15.java:13)
/* C_16 : printing matrix 2d array */
public class C_16 {
public static void main(String[] args) {
System.out.println("Matrix Array");
int[][] b;
b = new int[2][3];
b[0][0]=10; b[0][1]=20; b[0][2]=30;
b[1][0]=40; b[1][1]=50; b[1][2]=60;
for( int k=0;k<b.length;k++){
for( int l=0;l<b.length;l++){
System.out.print(b[k][l]+" ");
}
System.out.println();
}
}
}
// Matrix Array
// 10 20
// 40 50
/* C_17 : Printing jagged 2d array */
public class C_17 {
public static void main(String[] args) {
System.out.println("Jagged Array");
int[][]a = { {10,20,30,40} , {50,60,70} }; /* 0 index= 4 elemet,1 = 3 element of array*/
for( int i=0;i<a.length;i++){
for( int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}
// Jagged Array
// 10 20 30 40
// 50 60 70
/* C_21 lenght for 3d jagged and martrix array*/
public class C_21 {
public static void main(String[] args) {
int[][][] a = new int[2][3][4];
System.out.println(a.length); // 2
System.out.println(a[0].length); // 3
System.out.println(a[0][0].length); // 4
/* for jagged array */
int[][][] b = new int[3][][]; // contain 3 row
b[0]=new int[3][2]; // 0 index contain an array of 3 element further contian 2 element each
b[1]=new int[1][2]; // 1 conatins array of one element further contains 2 element
b[2]=new int[2][]; // 2 contains array of 2 element further contains 2 element contains
b[2][0] = new int[2]; // 2 element
b[2][1] = new int[3]; // 3 element
System.out.println("b = "+b.length);
System.out.println("b = "+b[1].length);
System.out.println("b = "+b[0][1].length );
}
}
// 2
// 3
// 4
// b = 3
// b = 1
// b = 2
/* C_22 : 3d array code */
public class C_22 {
public static void main(String[] args) {
int[][][] a = {{{10,20},{30,40,50,60},{70,80,90}}}; /* jagged array */
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
for(int k=0;k<a[i][j].length;k++){
System.out.print(a[i][j][k]+" ");
}
System.out.println();
}
}
}
}
// 10 20
// 30 40 50 60
// 70 80 90
/* C_24 : 4d array */
public class C_24 {
public static void main(String[] args) {
System.out.println("for 4d dimensions array");
C_24.sum3(new int[][][][] { { { {1,2,3},{4,5,6} } } } );
}
static void sum3(int[][][][] no3){
int total3=0;
for(int i[][][]:no3){
for(int j[][]:i){
for(int k[]:j){
for( int m:k){
total3 = total3+m;
}
}
}
System.out.println("sum of 4d array is = "+total3);
}
}
}
// for 4d dimensions array
// sum of 4d array is = 21
/* C_25 : 1d while loops and do while */
public class C_25 {
public static void main(String[] args) {
int[] a= {10,20,30};
int[] b= {40,50,60};
System.out.println("whiles loops");
int i=0;
while(i<a.length){
System.out.println(a[i]);
i++;
}
System.out.println("do while");
int j=0;
do{
System.out.println(b[j]);
j++;
}
while (j<a.length);
}
}
// whiles loops
// 10
// 20
// 30
// do while
// 40
// 50
// 60