Hi very one could someone help me with "delete method for Array"
I wrote this method in class Array but I didn’t know how could I decrement the size of array and I couldn’t make it work

this is the method

public void delete(int c)
	{	
		for (int i=0;i<a.length;i++){
			if(a[i]==c)
				a[i]=a[i+1];
		else 
			System.out.println(" not found");
			
			
	}

and this it the calling in the main

System.out.println("enter the element that u want to delete it ");
	int y=cin.nextInt();
	a.delete(y);

You cannot decrement the size of the array. Arrays have a fixed size in Java.

If the size of the array must remain accurate to the content, you could create a new array and then copy all but the deleted element from the old array into the new array.

Note that the code you have above is only moving the next item in the array, when you should probably move every item that comes after the one that was deleted.

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.