Home Java Example Java Core How To Remove Array Element In Java

Related Tutorials


 
 

Share on Google+Share on Google+

How To Remove Array Element In Java

Advertisement
In this section we will discuss about how to remove a given array element.

How To Remove Array Element In Java

In this section we will discuss about how to remove a given array element.

In this section we will discuss about removing an integer array element. We will discuss about to remove a specific position element. In this tutorial I have used a simple integer array which contains some integer element.

Example

Here I am giving a simple example which will demonstrate you about how to delete an element from array. To remove the array element I have used a technique that copy all the elements before the element you want to remove and again copy all the elements after the element you want to remove and then merged them to a single array. This technique is working well but you will have to care about the size of the array because the array can't be resized. In this example to copy the array I have used System.arrayCopy() method. Read detail about the System.arrayCopy() method from here. In this section an integer array is given which 3rd and 5th position element I want to remove. The sample code is given below.

Source Code

JavaRemoveArrayElementExample.java


public class JavaRemoveArrayElementExample { 

public static void main(String args[])
{
int num[]= {4,34,5,3,6,8,1};
int tempArray[]= new int[num.length];
int tempArray1[]= new int[num.length-3];
int tempArray2[]= new int[num.length-5];
int tempArray1And2[] = new int[(tempArray1.length+
tempArray1.length)-2];
int tempArray3[] = new int[tempArray1And2.length-3];
int tempArray4[] = new int[tempArray1And2.length-4];
int tempArray3And4[] = new int[tempArray3.length+
tempArray4.length];

System.out.println("");
System.out.println("Original List : ");
for(int i = 0; i<num.length; i++)
{
System.out.print(num[i]+" ");
}
System.out.println("");

System.arraycopy(num, 0, tempArray, 0, num.length);
//Removing 3rd position element from original array
System.arraycopy(num, 0, tempArray2, 0, num.length-5);
System.arraycopy(tempArray, 3, tempArray1, 0, 
tempArray.length-3);

System.out.println("Array list after removing 3rd " +
"position \n element from the original array list");
System.out.println("First list : ");
for(int i=0; i<tempArray2.length; i++)
{
System.out.print(tempArray2[i]+" ");
}
System.out.println("");

System.out.println("Second list : "); 
for(int i=0; i<tempArray1.length; i++)
{
System.out.print(tempArray1[i]+" "); 
}
System.out.println("");
//Merging array list tempArray2 and tempArray1
System.arraycopy(tempArray2, 0, tempArray1And2, 
0, tempArray2.length);
System.arraycopy(tempArray1, 0, tempArray1And2, 
tempArray2.length, tempArray1.length);

System.out.println("Merger Array First list and Second list");
for(int i =0; i<tempArray1And2.length; i++)
{
System.out.print(tempArray1And2[i]+" ");
}
System.out.println("");
//Removing 5th position element from original array
System.arraycopy(tempArray1And2, 0, tempArray3, 0, 
tempArray1And2.length-3);
System.arraycopy(tempArray1And2, 4, tempArray4, 0, 
tempArray1And2.length-4);

System.out.println("Array list after removing " +
"5th position \n element from the original list");
System.out.println("Third list : ");
for(int i=0; i<tempArray3.length; i++)
{
System.out.print(tempArray3[i]+" "); 
} 

System.out.println("");
System.out.println("Fourth list : "); 
for(int i=0; i<tempArray4.length; i++)
{
System.out.print(tempArray4[i]+" "); 
}
System.out.println("");
//Merging array list tempArray3 and tempArray4
System.arraycopy(tempArray3, 0, tempArray3And4, 0, 
tempArray3.length);
System.arraycopy(tempArray4, 0, tempArray3And4, 
tempArray3.length, tempArray4.length);
System.out.println("Final array list after removing 3rd " +
"\n and 5th position elements from the original list");
for(int i = 0; i <tempArray3And4.length; i++)
{
System.out.print(tempArray3And4[i]+" ");
}
System.out.println("");
}
}

Output

When you will execute the above example you will get the output as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 2, 2013

Related Tutorials

Discuss: How To Remove Array Element In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com