How to delete an element in an array?

Using an ArrayList or a Vector

Page 1 of 1

12 Replies - 140670 Views - Last Post: 24 February 2011 - 08:25 PM Rate Topic: -----

#1 n00bc0der  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

How to delete an element in an array?

Posted 27 November 2008 - 09:03 PM

public void returnAV(LendingItem itemIn) {
		
		for (int i = 0; i < 7; i++) {
			
			if (lentItemsData[i] == itemIn) {
				
			}
		}
	}



How can I delete an element in an array ?

I haven't been attending my CS class in a while and should know this lol.
thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: How to delete an element in an array?

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,191
  • Joined: 11-December 07

Re: How to delete an element in an array?

Posted 27 November 2008 - 09:11 PM

What do you mean by delete? You could set it to null or zero or an empty string depending on the type of array... but then you've still got that null, zero or empty string in the array.

If that's not what you need, you can delete it by shifting all the later elements in the array down one. For example, if you've got an array with indexes zero to 10, and you want to delete number 6:

copy number 7 to number 6 (this will overwrite the old value)
copy number 8 to number 7
copy number 9 to number 8
copy number 10 to number 9
You can do all that part with a for loop.

Now you need to deal with number 10. You could either just set it to null (or whatever) or you could create a new array with indexes running from zero to 9 and copy the old one into it.

Hope that helps!
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: How to delete an element in an array?

Posted 27 November 2008 - 09:14 PM

You could use a vector or such and can really "erase" a location within the data structure.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to delete an element in an array?

Posted 27 November 2008 - 09:21 PM

View Postn00bc0der, on 27 Nov, 2008 - 08:03 PM, said:

How can I delete an element in an array ?


You just can't

If you have an array of objects you can set its cell to null
If you have an array of positive integers you can set its cell to -1
Then it will be your program responsability to determine if the cell should be considered empty or not

As KIA said for that type of thing use an ArrayList or a Vector
Was This Post Helpful? 0
  • +
  • -

#5 n00bc0der  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Re: How to delete an element in an array?

Posted 27 November 2008 - 09:31 PM

Ok so i made a new array like said
and it's working fine:
	public void returnAV(LendingItem itemIn) {
		
		for (int i = 0; i < 7; i++) {
			
			if (lentItemsData[i] == itemIn) {
				LendingItem temp[] = new LendingItem[7];
				for (int j = 0; j < i; j++) {
					temp[j] = lentItemsData[j];
				}
				
				for (int k = i; (k > i) && (k < 7); k++) {
					temp[k-1] = lentItemsData[k];
				}
				
				lentItemsData = temp;
			}
		}
	}


Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5348
  • View blog
  • Posts: 27,305
  • Joined: 10-May 07

Re: How to delete an element in an array?

Posted 27 November 2008 - 09:42 PM

** Edited the topic to add more description **
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to delete an element in an array?

Posted 27 November 2008 - 09:47 PM

View Postn00bc0der, on 27 Nov, 2008 - 08:31 PM, said:

Ok so i made a new array like said
and it's working fine:
	public void returnAV(LendingItem itemIn) {
		
		for (int i = 0; i < 7; i++) {
			
			if (lentItemsData[i] == itemIn) {
				LendingItem temp[] = new LendingItem[7];
				for (int j = 0; j < i; j++) {
					temp[j] = lentItemsData[j];
				}
				
				for (int k = i; (k > i) && (k < 7); k++) {
					temp[k-1] = lentItemsData[k];
				}
				
				lentItemsData = temp;
			}
		}
	}


If you are removing an element from an array of size 7 shouldn't the new array have a size of 6 ?
Was This Post Helpful? 0
  • +
  • -

#8 n00bc0der  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Re: How to delete an element in an array?

Posted 27 November 2008 - 10:04 PM

Well here's the real time an example
i'm creating an array of objects of DVD's or films.
so a person can return a dvd, but their maximum limit is still 7.
so therefore the new array would still have 7.
if somethings wrong here please let me know, thanks guys/girls!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to delete an element in an array?

Posted 27 November 2008 - 10:12 PM

View Postn00bc0der, on 27 Nov, 2008 - 09:04 PM, said:

Well here's the real time an example
i'm creating an array of objects of DVD's or films.
so a person can return a dvd, but their maximum limit is still 7.
so therefore the new array would still have 7.
if somethings wrong here please let me know, thanks guys/girls!

So you are just moving the empty spot at the end of the array
No need to create a new array for that

if remove array index 3
move last used element to 3
set last element to not used

This was cfoley bright suggestion

This post has been edited by pbl: 27 November 2008 - 10:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to delete an element in an array?

Posted 27 November 2008 - 10:25 PM

View Postn00bc0der, on 27 Nov, 2008 - 08:31 PM, said:

Ok so i made a new array like said
and it's working fine:

Sorry "like said by cfoley" was not to make a new array but to shift the array cells
Was This Post Helpful? 0
  • +
  • -

#11 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,191
  • Joined: 11-December 07

Re: How to delete an element in an array?

Posted 28 November 2008 - 05:30 AM

It's OK pbl. I'm not comfortable taking credit for an algorightm that must have been in use millennia before I was born. ;)
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Praveen Kumar*


Reputation:

Re: How to delete an element in an array?

Posted 24 February 2011 - 02:48 AM

private String[] removeElementsFromArray(String[] elementsList, ArrayList<String> removeList){
        
        ArrayList<Integer> indexes = new ArrayList<Integer>();
        
        for( int i=0; i<elementsList.length; i++){
            if(removeList.contains(elementsList[i])){
                indexes.add(i); //Keep positions to remove from an array
            }
        }
        
        // Define an array - reduce size by no. of elements to remove
        String[] filteredActions= new String[elementsList.length-indexes.size()]; 
                
        int srcPos=0;
        int destPos=0;
        int noOfChars=0;
        for(Integer pos:indexes){
            noOfChars = pos-srcPos; //No of characters to copy
            System.arraycopy(elementsList,srcPos,filteredActions,destPos,noOfChars); // copy to newly defined array
            srcPos = srcPos+noOfChars+1; // Skip element from source by adding one to it
            destPos = destPos+noOfChars; // Next copying starts from index till that copied in previous call
        }
        System.arraycopy(elementsList,srcPos,filteredActions,destPos,(elementsList.length-srcPos));// Copy if anything remains
        
        return filteredActions;
    }


Was This Post Helpful? 0

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to delete an element in an array?

Posted 24 February 2011 - 08:25 PM

This thread is more than 2 years old please do not necropost thread
Don't jeopardize other's threads neither. Start your own
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1