File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Changing removeAt method to swap instead of removing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing removeAt method to swap instead of removing" Watch "Changing removeAt method to swap instead of removing" New topic
Author

Changing removeAt method to swap instead of removing

Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
I have this assignment where we are to change the removeAt method to swap with the last element in the list with the item to be removed. This is the original code.


results of this code are:

Enter 8 integers in the same line: 1 2 3 4 5 6 7 8

The list you entered is: 1 2 3 4 5 6 7 8

Enter the position of the num to be deleted: 5

After removing the element at position 5, intList :
1 2 3 4 5 7 8



I tried this for code:


This is the results of that code:

Enter 8 integers in the same line: 1 2 3 4 5 6 7 8

The list you entered is: 1 2 3 4 5 6 7 8

Enter the position of the num to be deleted: 5

After removing the element at position 5, intList :
1 2 3 4 5 null 8 8


And tried:


with results of:

Enter 8 integers in the same line: 1 2 3 4 5 6 7 8

The list you entered is: 1 2 3 4 5 6 7 8

Enter the position of the num to be deleted: 3

After removing the element at position 3, intList :
1 2 3 null 5 6 7 8

On each of the attempts I've tried it is not swapping. I've test printed a view of the array and it is filling the removed with null because initially the array is larger than the inital 8 integers, the last value in the array is null, but it is not swapping with the 4. The value 4 is still being removed and replaced with null. Is that how swapping is suppose to work? I assumed swapping meant that it would for example move the 4 into the last position of the array and then take the value from the last position and move it to where the 4 was? If someone could better explain this to help me out I'd really appreciate it. So confused right now.

Thanks in advance,
Shananne
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

I assumed swapping meant that it would for example move the 4 into the last position of the array and then take the value from the last position and move it to where the 4 was? If someone could better explain this to help me out I'd really appreciate it. So confused right now.


Well, you almost described what you need to do... You need to take the value from the 4th position, and store it in the last position. But before, you store it in the last position, you need to make a copy of the value in the last position. This is so that you can then store it in the 4th position.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
I tired this but now I get a null pointer exception.



Or am I really way off from doing this right?
Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Could anyone tell me if this seems right?



The results end up being:


Processing the integer list.
Enter 8 integers in the same line: 1 2 3 4 5 6 7 8

The list you entered is: 1 2 3 4 5 6 7 8

Enter the position of the num to be deleted: 1

After removing the element at position 1, intList :
1 8 3 4 5 6 7

It seems to be working right, but I'm not totally sure if I did it correctly? If it is, any input would be great for any suggestions as to how this can be done any other way that may be better. As far as this program goes, this would be impossible to do in and ordered list, right? Shifting would be the only way to perform removeAt and maintain an ordered list right?

Thanks,
Shananne
Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
Hello,

as far as I can understand, you are doing the following:
1.user enters an position
2.swap the element at the position entered by the user with the last element in the array.

If this is the case then it is simple

The removeAt method will look as follows:



public void removeAt(int location) {

if(location < 0 || location >= length)
System.err.println("The location of the item to be removed "
+ "is out of range.");
else
{
int lastElement = list[list.length-1];
list[list.length -1] = list[location];
list[location] = lastElement;
}
}
Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Originally posted by Gurps Bassi:
Hello,

as far as I can understand, you are doing the following:
1.user enters an position
2.swap the element at the position entered by the user with the last element in the array.

If this is the case then it is simple

The removeAt method will look as follows:



public void removeAt(int location) {

if(location < 0 || location >= length)
System.err.println("The location of the item to be removed "
+ "is out of range.");
else
{
int lastElement = list[list.length-1];
list[list.length -1] = list[location];
list[location] = lastElement;
}
}


I applied this to the code as you stated and got the errors as:

ArrayListClass.java:157: incompatible types
found : DataElement
required: int
int lastElement = list[list.length-1];
^
ArrayListClass.java:159: incompatible types
found : int
required: DataElement
list[location] = lastElement;
^
2 errors
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi Shananne,
in the place where int is used for last element, use that type which you are using in your list array, from the error it seems to be of DataElement type,
DataElement lastElement = list[list.length-1];

hope this solves your issue.


thanks,<br />gaurav abbi
Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Thanks!! This resolved the issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing removeAt method to swap instead of removing