Skip to main content

Array - Java Programming

//Array Is a Basic Data Structure in Computer Programming. Array Have Continues Memory Allocation
in Memory Space. Data Are Stored In Continues memory space . We Access Using the index Of The
Element. Index Start From 0 to n-1

//Basic Operations in Array

/*

  1. traverse
  2. search
  3. find
  4. insert
  5. Remove


*/


class array{

//Traverse A Array
public static void traverse(int[] arr){
int n=arr.length;
for(int i=0;i<n;i++){
System.out.println(arr[i]);
}
}


//Find Value By Index
public static void findByIndex(int[] arr,int index){
System.out.println(arr[index]);
}


//Find Index By Value
public static void findIndex(int[] arr,int value){
int n=arr.length;
for(int i=0;i<n;i++){
if(arr[i]==value){
System.out.println(i);
}
}
}

//Insert Function (Array Copy Method)
public static void insert(int[] arr,int index,int value){
int n=arr.length;
int[] brr=new int[n+1];

for(int i=0;i<index;i++){
brr[i]=arr[i];
}
int j=index;
while(j<n){
brr[j+1]=arr[j];
j++;
}
brr[index]=value;
traverse(brr);
}

//Remove Element By Index (Array Copy Method)
public static void removeByIndex(int[] arr,int index){
int n=arr.length;
int[] brr=new int[n-1];

for(int i=0;i<index;i++){
brr[i]=arr[i];
}

for(int i=index;i<n-1;i++){
brr[i]=arr[i+1];
}
traverse(brr);
}

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

//treaverse array
//traverse(arr);
//findByIndex(arr,3);
//insert(arr,2,5);
removeByIndex(arr,2);
}
}

Comments

Popular posts from this blog

Class is a collection of similar objects. Which have common properties

Class is a collection of similar objects. Which have common properties. Syntax class class_name {        //member functions and variables. } How to create a objects for class class_name c=new class_name; We can access the member variable and member methods using dot operator. c.member_function(); Eg : class bike { void fun() { System.out.println("member method"); } public static void main(String[] args) { bike B=new bike(); B.fun(); } } output member method

Linked List - Data Structure Basic With Example Java

//linked list /*     Linked List is a Linear Data Structure.      Drawback of Array Is continues Block Of Memory. Linked Is Used To Allocate the Different Memory Location. Each node Contains Data And Pointer. Header Is Used To Denote The First Node Of Linked List. Tail Is Used To Denote the Last Node of The Linked List. 1.Singly Linked List Each Node as Data And pointer To The Next Node. [ data(header) | pointer of next]->[ data | pointer of next]->[ data | pointer of next]->[ data | null]; 2.doubly Linked List Each Node as Data And pointer To The Next Node and prev Node. [ data(header) | pointer of next]<->[pointer to prev|data | pointer of next]<->[pointer to prev|data | pointer of next]<->[pointer to prev|data | null]; 3.Circle Linked List [ data(header) | pointer of next]->[ data | pointer of next]->[ data | pointer of next]->[ data | null]-->header */ class...