Write a program in Java to implement ArrayList and use the important mrthods like add(), size(),isEmpty() etc

Write a program in Java to implement ArrayList and use the important mrthods like add(), size(),isEmpty() etc


import java.util.ArrayList;
class hunteye {
    public static void main(String[] args) {
      ArrayList obj = new ArrayList();
      // isEmpty() will return true here as obj is empty 
      System.out.println(obj.isEmpty()); // true
      //size() will return 0
       System.out.println(obj.size()); //0
       // adding int
       obj.add(12);
       // adding float
       obj.add(567.123);
       //adding char
       obj.add('$');
       //adding string 
       obj.add("Java is OOPS");
       //printing all element
       System.out.println(obj);
        // isEmpty() will return false here as obj is not empty 
      System.out.println(obj.isEmpty()); // false
     //size() will return 4
       System.out.println(obj.size()); //4
      //contains(12) will return true
      System.out.println(obj.contains(12)); 
      //remove() to remove  element from ArrayList 
      obj.remove(0);// deleting element from 0 Position
      //printing all element
      System.out.println(obj);
    }
}

Output

true
0
[12, 567.123, $, Java is OOPS]
false
4
true
[12, $, Java is OOPS]

Share:

Explain the ArrayList class in java with example

ArrayList class in Java

Definition

ArrayList is a predefined java class under java util package

Advantage

ArrayList is a generic dynamic array. Which means it can be of anytype or of any size

Some important methods of ArrayList class

Methods Purpose
add() It is used to add elements in ArrayList
size() It returns the size of ArrayList
isEmpty() It returns true if ArrayList is Empty, otherwise false
contains() It is used to search an element inside ArrayList and returns true if found, otherwise false
remove() It is used to delete an element from ArrayList

WAP in java to implement ArrayList and use the important mrthods like add(), size(),isEmpty() etc


import java.util.ArrayList;
class hunteye {
    public static void main(String[] args) {
      ArrayList obj = new ArrayList();
      // isEmpty() will return true here as obj is empty 
      System.out.println(obj.isEmpty()); // true
      //size() will return 0
       System.out.println(obj.size()); //0
       // adding int
       obj.add(12);
       // adding float
       obj.add(567.123);
       //adding char
       obj.add('$');
       //adding string 
       obj.add("Java is OOPS");
       //printing all element
       System.out.println(obj);
        // isEmpty() will return false here as obj is not empty 
      System.out.println(obj.isEmpty()); // false
     //size() will return 4
       System.out.println(obj.size()); //4
      //contains(12) will return true
      System.out.println(obj.contains(12)); 
      //remove() to remove  element from ArrayList 
      obj.remove(0);// deleting element from 0 Position
      //printing all element
      System.out.println(obj);
    }
}

Output

true
0
[12, 567.123, $, Java is OOPS]
false
4
true
[12, $, Java is OOPS]

Share:

Object in JAVA and it's uses

What is Object in Java and what are it's Uses?

Definition

A class instance is known as Objects. In java object is the runtime entity of a class

Uses

Object is used to access the class members (both data member and member function)

Example



class hunteye {

    //data member
    int x ; 
    
    //member function
    void set(){
        x=13;
    }
    
    void get(){
        System.out.println("x = "+x);
        
    }
    
    public static void main(String[] args) {
    
      hunteye obj = new hunteye(); //object is created here 
      obj.set(); //object is access member function here
      obj.get();
    }
}

Share:

Write a program in Java to print Fibonacci Series using Recursion

Write a program in Java to print Fibonacci Series using Recursion


public class Fibonacci {
    public static void printFibonacci(int count, int a, int b) {
        int c = a + b;
        if (count == 0) {
            return;
        }
        System.out.print(c + "\t");
        a = b;
        b = c;
        printFibonacci(count - 1, a, b);
    }

    public static void main(String[] args) {
        int count = 8;
        System.out.print(0 + "\t" + 1 + "\t");
        printFibonacci(count - 2, 0, 1);
    }
}


Output

0 1 1 2 3 5 8 13

Share:

Write a program in Java to Calculate Factorial of a number using Recursion

Write a program in Java to Calculate Factorial of a number using Recursion


public class Factorial {

    public static int calcFact(int n) {
        if (n == 1 || n == 0) {
            return 1;
        }
        int fact = calcFact(n - 1);
        return n * fact;
    }

    public static void main(String[] args) {

        System.out.print(calcFact(5));
    }
}


Output

120

Share:

Write a Program in Java to demonstrate Selection Sort

Write a Program in Java to demonstrate Selection Sort


public class SelectionSort {
    public static void sort(int arr[]) {
        for (int i = 0; i < arr.length - 1; i++) {
            int pos = i; //Position of minimum element
            System.out.println("Pass " + (i + 1));
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[pos]) {
                    pos = j;
                }
            }
            int tmp = arr[i];
            arr[i] = arr[pos];
            arr[pos] = tmp;
            display(arr);
        }
    }

    public static void display(int arr[]) {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
        System.out.println("***********************************\n");
    }

    public static void main(String args[]) {
        int arr[]={56,23,67,11,45};
        sort(arr);
    }
}


Output

Pass 1
11 23 67 56 45
***********************************

Pass 2
11 23 67 56 45
***********************************

Pass 3
11 23 45 56 67
***********************************

Pass 4
11 23 45 56 67
***********************************

Share:

Write a Program in Java to demonstrate Insertion Sort

Write a Program in Java to demonstrate Insertion Sort


public class InsertionSort {
    public static void sort(int arr[]) {

        for (int i = 1; i < arr.length; i++) {
            System.out.println("Pass " + (i));
            int j = i - 1;
            int key = arr[i];
            while (j >= 0 && key < arr[j]) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
            display(arr);
        }

    }

    public static void display(int arr[]) {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
        System.out.println("***********************************\n");
    }

    public static void main(String args[]) {
        int arr[] = { 5, 1, 4, 2, 8 };
        sort(arr);
        // display(arr);
    }
}


Output

Pass 1
1 5 4 2 8
***********************************

Pass 2
1 4 5 2 8
***********************************

Pass 3
1 2 4 5 8
***********************************

Pass 4
1 2 4 5 8
***********************************

Share:

Write a Program in Java to demonstrate Bubble Sort

Write a Program in Java to demonstrate Bubble Sort


public class BubbleSort {
    public static void sort(int arr[]) {
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.println("Pass " + (i + 1));
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }

                display(arr);
            }
            System.out.println("***********************************\n");
        }
    }

    public static void display(int arr[]) {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

    public static void main(String args[]) {
        int arr[] = { 5, 1, 4, 2, 8 };
        sort(arr);
        // display(arr);
    }
}

Output

Pass 1
1 5 4 2 8
1 4 5 2 8
1 4 2 5 8
1 4 2 5 8
***********************************

Pass 2
1 4 2 5 8
1 2 4 5 8
1 2 4 5 8
***********************************

Pass 3
1 2 4 5 8
1 2 4 5 8
***********************************
Pass 4

1 2 4 5 8
***********************************

Share:

Pageviews