Hi. Can anyone help me on my code? I wanted to delete a data inside a array, so what I did was to move the the array into another array without the one that is to be deleted. It is working if the user only wants to delete a single record. How can I improve my code without using vectors?

OR is there a better way of deleting data inside an array?

case 3:
{
System.out.println("Delete a array entry: ");
int d = Integer.parseInt(input.readLine());
while (i < a.length && j < b.length) {
if (d == a[i]) {
i++;
}
else {
b[j] = a[i];
i++;
j++;
}
}
break;
}
case 4:
{
System.out.println("View All");
for(i=1;i<a.length;i++)
{
if(a[i] == 0){
break;
}
else
System.out.println(a[i]);
}
break;
}

Recommended Answers

All 3 Replies

To tell you the truth, you would be much better off using an ArrayList, otherwise, place this code into a method that accepts an array and an element as arguments, and returns an array, then call this method for each element to be removed (is one fairly inefficent way). Or, place all elements to be removed in another array, then loop over the original array, and inside that, loop over the smaller array and comparing them (another ineffecient way).

Can you refer me to any ArrayList tutorial or some thing like that?

http://java.sun.com/

Go to documentation, you'll find the Collections tutorials (as well as all the others) somewhere under there.

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.