14 Replies - 818 Views - Last Post: 13 September 2011 - 01:41 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Deleting elements from an array

Posted 13 September 2011 - 10:32 AM

I wan tto write a method that deletes elements from an array.

The method should have two parameters. The starting index to be deleted ( first parameter), and number of elements to be deleted ( call it nums) .

So, lets say we name numitems as the number of elemtns in the array.

I know how to delte on element,but I am having trouble to understand how to delete two or more elements...


Then here's what i have done:
 public void delete( int index, int nums) 
{ 
    
   if( index < numitems && index+nums < numitems) 
   { 

    // If it wa sjust one element, I would do this
      // for(int i = index; i < numitems-1; i++) 
      //  list[i] = list[i+1]; 
      //  numitems--; 
     
     // for multiple one, i tried it this way
 
       for(int i = index; i < index+nums;i++) 
        {  
		  list[index] = list[index+nums+1]; 
          numitems--; 
        }


To my understanding, I thought this would work,but it doesnt and I dont know why.
I am trying to shif all the elements left by one starting from index and at the same time decrementing number of items.
I would appreciate your feedback.

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting elements from an array

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Deleting elements from an array

Posted 13 September 2011 - 10:48 AM

Array? Why?
First you can only change places of values but you cant shrink the array...
Second your loop is not logically correct, lets say the array is {1,2,3,4,5} and index = 1 and nums =2, your loop will do the following changes {1,5,3,4,5}
what you are decrement is not the real size of the array, it is just the copy of it.
Third this if is redundant if( index < numitems && index+nums < numitems) why not just if(index+nums < numitems)
Also this line may go out of bounds
 list[index] = list[index+nums+1]; 


Why not using arrayList?
Was This Post Helpful? 0
  • +
  • -

#3 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Deleting elements from an array

Posted 13 September 2011 - 10:53 AM

Thanx for your answer. In this case, I am required to use an array. So, the thing I need help on is a loop to remove more than one element...
Any idea?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,119
  • Joined: 19-March 11

Re: Deleting elements from an array

Posted 13 September 2011 - 11:04 AM

       for(int i = index; i < index+nums;i++) 
        {  
		  list[index] = list[index+nums+1]; 
          numitems--; 
        } 


This is a little odd. What you're doing is setting list[index] to list[index+nums+1], (index+nums+1) times, and subtracting index+nums+1 from numitems.
Are you sure that's what you want?



More generally, I think if you try to explain the logic that you want to execute you'll find it easier to see what you need to do.

So let's take this array {1,2,3,4,5,6,7} as a point to work from.

Walk through how you should execute delete (1,2) on this. If you can explain it, you can probably do it.

This post has been edited by jon.kiparsky: 13 September 2011 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Deleting elements from an array

Posted 13 September 2011 - 11:07 AM

Suppose we have a list {1,2,3,4,5} . If I want to remove 2,3,4..I would remove 2,3,4 (if I were to use arraylist,but how about in array..??) How do you remove these numbers?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,119
  • Joined: 19-March 11

Re: Deleting elements from an array

Posted 13 September 2011 - 11:12 AM

That's the question, isn't it?
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Deleting elements from an array

Posted 13 September 2011 - 11:18 AM

As macos said, you cannot remove an index from an array once set it is set. What you can do is make a for loop, starts at the index they give you, goes for the numbers to remove. Since you cannot delete it closest you can get is set the value to null.

Edit: You could also have the method return an array, where you create a new array in the method, with the current array length - num to remove. Then cycle through with a for loop and assign the new values. Then return the array. Also, to get the main array you need to pass it in the params so it will take a 3rd parameter.

When you call it in the main method, since it returns an array you would have to assign the main array to the method. ie mainArray = deleteArray(mainArray, 0,2);

This post has been edited by Fuzzyness: 13 September 2011 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Deleting elements from an array

Posted 13 September 2011 - 11:30 AM

View PostFuzzyness, on 14 September 2011 - 12:03 AM, said:

As macos said, you....

Duh! ;)

Quote

Suppose we have a list {1,2,3,4,5} . If I want to remove 2,3,4..I would remove 2,3,4 (if I were to use arraylist,but how about in array..??) How do you remove these numbers?
What you can do is setting them another value but you cant shrink the array.
Was This Post Helpful? 0
  • +
  • -

#9 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Deleting elements from an array

Posted 13 September 2011 - 11:33 AM

I am only allowed to pass two params in my method: So here's my another version.
public void delete(int index, int nums)
  { 
    T [] list2 = (T []) new Object[list.length];
	if(nums+index < numitems) 
    { 
	
	  
	  T temp;
       for(int i = 0; i < numitems; i++) 
       { 
		 
		 if( i >= index && i <= index+nums) 
           continue; 
          else 
            list2[i] = list[i];  
        } 



The issue now is to eleminate the null values in my array ...Any ideas?
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Deleting elements from an array

Posted 13 September 2011 - 11:36 AM

Create another for loop with an if statement if(!null) and then add it in to the new array.

@smohd - Duh? I'm confused for some reason ^^

This post has been edited by Fuzzyness: 13 September 2011 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,119
  • Joined: 19-March 11

Re: Deleting elements from an array

Posted 13 September 2011 - 11:48 AM

To the OP:

(1)The question is, are you deleting for local use, or returning an array with the elements deleted?
If you're maintaining values in an array and tracking where the last value is, and using the array locally, that's one problem. If you're returning an array with a slice removed, that's another.

(2) Why are you screwing around with generics when, based on the way you're writing the method, you have to know what the array is at compile time?

To smohd and fuzzy:
(3) "You can't shrink the array" - no, but creating a new array and returning it is easy enough, if that's what you need to do. You do the same thing for growing an array, and that's also a normal practice. Let's not spend so much time on what might be better practice when obviously the guy's just trying to get a hint (hint! that is) on how to finish a piece of homework.

(4) "Why not use an ArrayList?" Well, obviously this is a homework assignment and the problem is to solve a problem. But if you're maintaining a list of primitives, an ArrayList would force autoboxing, which is probably going to eat up more performance than occasional deletes, so there are cases where this would be a useful technique. Also, array access is faster than a method call, generally.
Was This Post Helpful? 0
  • +
  • -

#12 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Deleting elements from an array

Posted 13 September 2011 - 11:56 AM

Thanx for your inputs. I want to return an array with the numbers remove.
I cant use an arraylist, I know that would be easy...

I know how to remove those elements, but how do you return it without the null value in?

Something like this .. is what I thought
T [] list2 = (T []) new Object[list.length];
	 T [] list3 = (T []) new Object[list.length];
	if(index < numitems && nums+index < numitems) 
    { 
	
	  
	  T temp;
       for(int i = 0; i < numitems; i++) 
       { 
		 
		 if( i >= index && i <= index+nums) 
           continue; 
          else
            list2[i] = list[i];  
        } 
        
		for(int i =0; i < list2.length; i++) 
		{ 
		   if(list2[i] !=null) 
		   list3[i] = list2[i]; 
		}


Was This Post Helpful? 0
  • +
  • -

#13 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Deleting elements from an array

Posted 13 September 2011 - 12:03 PM

In that case simply forget your nmenuitems
Simply used the array size
Create a new array every time an element is added or deleted


public class Array <T>{

	private T[] list;
	
	public Array() {
		list = (T[]) new Object[0];
	}
	
	public void add(T o) {
		T[] newList = (T[]) new Object[list.length + 1];
		for(int i = 0; i < list.length; i++)
			newList[i] = list[i];
		newList[list.length] = o;
		list = newList;
	}
	
	public void delete(int idx) {
		delete(idx, 1);
	}
	
	public void delete(int idx, int nb) {
		T[] newList = (T[]) new Object[list.length - nb];
		int k = 0;
		for(int i = 0; i < list.length; i++) {
			if(i < idx || i > idx + nb -1)
				newList[k++] = list[i];
		}
		list = newList;
	}
	
	public int getSize() {
		return list.length;
	}
}


This post has been edited by cmpshr: 13 September 2011 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,119
  • Joined: 19-March 11

Re: Deleting elements from an array

Posted 13 September 2011 - 12:18 PM

cmpshr - let's try not to do people's homework, okay? Thanks.

@imu - The only way to "resize" an array is to create and populate a new one of the correct size. (I'm omitting System.arraycopy for reasons that should be obvious). So whether you want to either return a new array or modify the one you're working on (ie, if you want to work by returning a value or by a side effect), the technique will be to
(1) determine the size of the new array and then (2) put the correct elements from the original array into it.

Then you'll either change the original reference to point to the new array (side-effect programming) or you'll return the new array and let the caller decide what to do with it (responsible programming)
Was This Post Helpful? 1
  • +
  • -

#15 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Deleting elements from an array

Posted 13 September 2011 - 01:41 PM

Thanks alot for your inputs. I benefited alot from this discussion.
I'll try to code it from scratch ans see if i can put the concept into
code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1