ARRAY PARACTICE

0




  1. // this is my first array progrmme i check how its work


    public class array {
        public static void main(String[] args) {
            int[]a = {7,8,8,2};
            System.out.println(a[0]);
        }
       
    }
     

  2. // if you have hundreds of data to for use loop in array and this array belongs to single dimensional array



    public class array1 {
        public static void main(String[] args) {
            int a[] = {2,7,8,5};
            for(int i=0; i<=3; i++)
            {
                System.out.println(a[i]);
            }
        }

    }

  3. // i am using input mehtode to collect data from use like bellow



    import java.util.Scanner;
    public class array2 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int a[] = new int [5];
            a[0]=sc.nextInt();
            a[1]=sc.nextInt();
            a[2]=sc.nextInt();
            a[3]=sc.nextInt();
            a[4]=sc.nextInt();
            System.out.print("this is output");
           
           
                for(int i=0;i<=4;i++)
                {
                    System.out.print(a[i]);
                }
           
            sc.close();

           
        }
       
    }


  4. // i am using string for arrary


    import java.util.Scanner;
    public class array3 {
        public static void main(String[] args) {
            Scanner sc = new Scanner (System.in);
            String Name []= {"Rakesh", "pankaj", "akshay"};
            for(int i=0; i<=2; i++)
            {
                System.out.println(Name[i]);

            }
            sc.close();

           
        }
       
    }


  5. // this is 2d array and this array creat using user input methode and new keword for not defined value


    import java.util.Scanner;
    public class Array2d {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int a [][]= new int [3][2];
            a [0][0]=sc.nextInt();
            a [0][1]=sc.nextInt();
            a [1][0]=sc.nextInt();
            a [1][1]=sc.nextInt();
            a [2][0]=sc.nextInt();
            a [2][1]=sc.nextInt();
            for(int i=0; i<=2; i++)
            {
                for(int j=0; j<=1; j++)
                {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println();
            }
            sc.close();
        }
       
    }
  6. // if i have a simple 2 d we can perfome some


    public class Array2D1 {
        public static void main(String[] args) {
            int nums [][] =
        {
            {5,8},
            {8,6},
            {7,3}
        };
        for (int i =0; i<=2; i++)
        {
            for( int j=0; j<=1; j++)
            {
                System.out.print(nums[i][j]+ " ");
            }
            System.out.println();
        }
        }
    }


  7. //jagged array



    public class jaggedArray {
        public static void main(String[] args) {
            int a[][]=
            {
                {8,7,5,3},
                {4,7},
                {7,6,8,3}

            };
            for(int i=0;i<=2;i++)
            {
                for(int j=0; j<a[i].length;j++)
                {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println();
           
            }
           
           
        }
       
    }

  8. //without declare data using new keyword

    public class JaggedArray1 {
        public static void main(String[] args) {
            int nums [][]= new int[3][];
            nums [0]=new int [4];
            nums [1]=new int [2];
            nums [2]=new int [3];
            nums [0][0]=5;
            nums [0][1]=5;
            nums [0][2]=5;
            nums [0][3]=5;
            nums [1][0]=5;
            nums [1][1]=5;
            nums [2][0]=5;
            nums [2][1]=5;
            nums [2][2]=5;
            for(int i=0;i<=2;i++)
            {
                for(int j=0; j<nums[i].length;j++)
                {
                    System.out.print(nums[i][j] + " ");
                }
                System.out.println();
           
            }
           


        }
       
    }

  9. // i am using enhance for loop

    public class JaggedArray1 {
        public static void main(String[] args) {
           int nums[][] =
           {
            {4,8,4,6},
            {7,8,2,3},
            {7,9,2,4},
            {8,6,8,2}
           };
            for(int a[] : nums)
            {
                for(int b : a)
                {
                    System.out.print(b + " ");
                }
                System.out.println();
           
            }
           


        }
       
    }

  10. // i am using enhance for loop and this is work in jagged array

    public class JaggedArray1 {
        public static void main(String[] args) {
           int nums[][] =
           {
            {4,8,4,6},
            {7,8,2},
            {7,9,2,4},
            {8,6,8}
           };
            for(int a[] : nums)
            {
                for(int b : a)
                {
                    System.out.print(b + " ");
                }
                System.out.println();
           
            }
           


        }
       
    }

Post a Comment

0Comments
Post a Comment (0)