how can i remove an element in my array without using special commands like hashmap....i have made many parts of it...just the delete part...

import java.io.*;
public class Super
{
	public static void main(String args[])throws IOException

	{
		String temp;
		int num[]=new int[10], choice,index,value,i,search,found=0;
		BufferedReader in;
		in = new BufferedReader(new InputStreamReader(System.in));
		for(i=0;i<10;i++)
		{
			System.out.print("Enter a Number: ");
			temp=in.readLine();
			num[i]=Integer.parseInt(temp);
		}
		do
		{
			System.out.println("\n[1]Insert");
			System.out.println("[2]Delete");	
			System.out.println("[3]Search");
			System.out.println("[4]View");
			System.out.println("[5]Exit");
			System.out.print("Enter Choice:");
			temp=in.readLine();
			choice=Integer.parseInt(temp);
			switch(choice)
			{
				case 1:
				
				System.out.print("Enter Index:");
				temp=in.readLine();
				index=Integer.parseInt(temp);
				System.out.print("Enter New Value:");
				temp=in.readLine();
				value=Integer.parseInt(temp);
				num[index]=value;
				System.out.println("The Values are:");
				for(i=0;i<10;i++)
					System.out.print(num[i] + " ");
				break;
				case 2://problem in deleteng...dont know how..
				System.out.print("Enter Index:");
				temp=in.readLine();
				index=Integer.parseInt(temp);
				index='\0';
				System.out.println("The Remaining Values are:");
				for(i=0;i<10;i++)
					System.out.print(num[i] + " ");
				break;
				case 3:
				System.out.print("Enter Number to be searched:");
				temp=in.readLine();
				search=Integer.parseInt(temp);
				for(i=0;i<10;i++)
				{
					if(num[i]==search)
					{
						found++;
					}
					
				}
				if(found==0)
					{
						System.out.println("No match Found!");
					}
				System.out.print("No. of Elements Found:" + found + "\n");
				break;
				case 4:
				System.out.println("The New Sets of Elements are: ");
				for(i=0;i<10;i++)
					System.out.print(num[i] + " ");
				break;
				case 5:
				System.out.println("End of Program.");
				break;
				default:
				System.out.println("Invalid Input!");
			}
		}while(choice!=5);
	}
}

To delete an element of an array shift all elements up by one index position starting from the next element from the one to be deleted so that the element to be deleted is overwritten.

For e.g. If you have an array: 4 9 13 10 17 3 20 and you want to delete the element 13, then starting from element after 13 (10) shift all elements one position to the left so that 13 is overwritten by 10, 10 by 17, 17 by 3 & 3 by 20. Having done this you would have deleted the element 13.

You could avoid this process using some of the methods from the Arrays Class, but this is just how to do it at a low-level.

thank its a big help...i just figured it...thank you very much for the help...

just another question..its not saving....if i delete an element its not saving...how will it save?

What do you exactly mean by saving ? Please elaborate.

eg.
4 9 13 10 17 3 20 i deleted 13....if i go back to main menu and select DELETE....i chose 4 to delete....13 shows up again...


heres my code in case 2...

System.out.print("Enter Index:");
				temp=in.readLine();
				index=Integer.parseInt(temp);
				if(index<10 && index>=0)
				{
					System.out.println("The Remaining Values are:");
					for(i=0;i<10;i++)
					{
						if(i!=index)	
						System.out.print(num[i] + " ");
					}
				}

Thats because you are not deleting the element, but just skipping over it during the print operation. You will actually need to shift the elements up by one position from after the element to be deleted for the element to actually get deleted from the array. as described by me in my first post.

oh i see....my code is not the same of what you stated in your first post??

No. it isn't.

You have to keep track of the length of your array and shift the elements all back one, like verruckt said, every time that you 'delete' an element. The true underlying size of the array is not changing.

but how?i can't execute it well..

You can keep track of the length of your array by having an int variable that stores the length, then every time you add something to your array, update it, and when you delete, also update it. To move everything back one space, all you need to do is have a for loop and a 'temp' variable to hold the element that you're about to overwrite (because if you overwrite it first, it is lost and can't be recovered - store it in a temp variable before overwriting).

If you can't figure out from that then show us some code. We aren't going to write it for you. (I.e. make an attempt yourself)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.