Array List in java

0

Information and study is a popular blog of programming languages (C,C++,java),Data structure and algorithms,object oriented programming, and interviewing questions for freshers. This blog is essential for software engineers and developers.


Array list in java



Array List in java



An array list is a data structure that is similar to an array, but allows for dynamic resizing. It is a combination of an array and a list, where elements can be added or removed from the list, and the underlying array will automatically adjust its size to accommodate the changes. Array lists are commonly used in programming languages such as Java and C#, and are often used to store collections of data. They offer constant-time access to elements by index, and allow for efficient insertion and deletion of elements. However, they can be less efficient than using a linked list when frequently inserting or deleting elements at the beginning or middle of the list.

Operations in array list:

  • createrName()
  • declarArray()
  • displayArray()
  • swapValuesArray()    
  • displayArray()
  • startValueArray()
  • endValueArray()
  • maximumArrayValue()
  • minimumArrayValue()
  • updateArray()
  • displayArray()
  • copyArray()
  • displayArray()
  • ArrayIsEmpty()
  • insertInArray()
  • displayArray()
  • insertAtInArray()
  • displayArray()
  • removefromArray()
  • displayArray()
  • removeAtArray()
  • displayArray()
  • searchInArray()
 

Code of Array list:

 

import java.util.Scanner;

public class arrlistJhangir

{

                int [] arr= new int [10];

                int length=0;

               

                public void createrName()

                {

                                System.out.print("Created by Jhangir Mughal\n\n");

                }

                public void declarArray()

                {

                System.out.println("Enter array length for declar");

                Scanner object = new Scanner(System.in); //create object only one time in one function

               

                length=object.nextInt();

                                for (int i = 0; i<length; i++)

                                {

                                System.out.println("\nEnter numbers in array");

                                arr[i] =object.nextInt();

                                }

                }

               

                public void displayArray()

                {

                System.out.println("\nEnter number in array is ");

                                for(int i= 0;i<length; i++)

                                {

                                                System.out.println(arr[i]);

                                }

                }

                public void swapValuesArray()

                {

                int index1;

                int index2;

                System.out.println("Enter index 1");

                Scanner obj = new Scanner(System.in);

                index1=obj.nextInt();

                System.out.println("Enter index 2");

                index2=obj.nextInt();

               

                int temp= arr[index1];

                arr[index1]=arr[index2];

                arr[index2]=temp;

                }

               

               

                public void startValueArray()

  {

   System.out.println("1st vlaue in array" +arr[0]);

  }

 

public void endValueArray()

{

System.out.println("last value in array" +arr[length-1]);

}

 

public void maximumArrayValue()

{

int max=arr[0];

int index=0;

for (int i=1;i<length;i++)

                {

                                if(arr[i]>max)

                                                {

                                                max=arr[i];

                                                index=i;

                                                }

                }

                System.out.println("\nLargest number in array is" + max);

                System.out.println("Largest number found in index " + index );

}

               

                public void minimumArrayValue()

{

int max=arr[0];

int index=0;

for (int i=1;i<length;i++)

                {

                                if(arr[i]<max)

                                                {

                                                max=arr[i];

                                                index=i;

                                                }

                }

                System.out.println("\nSmallest number in array is" + max);

                System.out.println("Smallest number found in index " + index );

}

 

public void updateArray()

{

                int num;

                int index;

                Scanner obje=new Scanner(System.in);

               

                System.out.println("\nEnter number in array");

                num=obje.nextInt();

                System.out.println("Enter index where number store");

                index=obje.nextInt();

                if(index>=0 && index<=length-1)

                arr[index]=num;

}

 

                public void copyArray()

                {

                int index1;

                int index2;

                System.out.println("Enter index 1 where you copy");

                Scanner objec= new Scanner(System.in);

                index1=objec.nextInt();

                System.out.println("Enter index 2 what you copy");

                index2=objec.nextInt();

                arr[index1]=arr[index2];

                }

               

                public void ArrayIsEmpty()

{

 

if (length ==0 )

                System.out.println("\nArray is empty");

                else

                System.out.println("\nArray is not empty");

}

               

                public void insertInArray()

{

                int num;

                Scanner objectt= new Scanner(System.in);

                System.out.println("Enter number that insert in array");

                num=objectt.nextInt();

    arr[length]=num;

    length++;

}

 

public void insertAtInArray()

{

                int index;

                int num;

                Scanner obje=new Scanner(System.in);

               

                System.out.println("Enter index where number store");

                index=obje.nextInt();

               

                System.out.println("\nEnter number");

                num=obje.nextInt();

               

int temp1=arr[index];

arr[index]=num;

for (int i=index+1;i<length;i++)

                {

                int temp2=arr[i];

                arr[i]=temp1;

                temp1= temp2;

                }

length++;

}

public void removefromArray()

{

arr[length-1]=0;

length--;

}

               

public void removeAtArray()

{

                int index;

                Scanner obje=new Scanner(System.in);

                System.out.println("Enter index where remove value");

                index=obje.nextInt();

for (int i=index; i <length;i++)

                arr[i]=arr[i+1];

arr[length-1]=0;

length--;

}

 

public void searchInArray()

{

                                int num;

                                Scanner objet= new Scanner(System.in);

                                System.out.println("\nEnter number");

                num=objet.nextInt();

for (int i=0;i<length;i++)

                {

                if (arr[i]==num)

                                {

                                System.out.println("Number is founded in                          index "+ i);

                                }

                }

}void insertionSort(int n)

{

                int[] arr1=arr;

                n=arr.length;

    int i, key, j;

    for (i = 1; i < n; i++)

    {

        key = arr1[i];

        j = i - 1;

 

   

        while (j >= 0 && arr1[j] > key)

        {

            arr1[j + 1] = arr1[j];

            j = j - 1;

        }

        arr1[j + 1] = key;

    }

                arr=arr1;

}

                public static void main(String[] args)

                {

                                arrlistJhangir object;

                                object= new arrlistJhangir();

                                object.createrName();

                                object.declarArray();

                                object.displayArray();

                                object.swapValuesArray();

                                object.displayArray();

                                object.startValueArray();

                                object.endValueArray();

                                                                     object.maximumArrayValue();

                                object.minimumArrayValue();

                                object.updateArray();

                                object.displayArray();

                                object.copyArray();

                                object.displayArray();

                                object.ArrayIsEmpty();

                                object.insertInArray();

                                object.displayArray();

                                object.insertAtInArray();

                                object.displayArray();

                                object.removefromArray();

                                object.displayArray();

                                object.removeAtArray();

                                object.displayArray();

                                object.searchInArray();

                               

}

}

Post a Comment

0Comments
Post a Comment (0)