• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList question  RSS feed

 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're given a array based list class and the list class has an array, capacity, and a size. We're assuming the size is always less than the capacity of the array and that size is the amount of values in the array based list.

I'm trying to do some "pseudo" coding to handle different scenarios.

Right now I'm trying to remove the last value of the array. I'm just looking at the last value and nothing else.



I just want to break this down a little bit and if I'm incorrect please notify me with what's wrong.



If there's no values in the Array List then send a message out indicating that the last value will not be removed.



If there's only one value in the Array List then remove that value leaving the Array List empty and assume that the array is using the standard 0 value in the first spot of the array.



If there's more than 1 value in the list then remove the last node in the list assuming that the array is using the standard 0 value in the first spot of the array.

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you treating 'size == 1' differently to 'size > 1'?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And is setting the element to null the right thing to do? What would happen if you remove the last element, and then remove the last element again?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 3 & 4 are redundant; if you remove them the program will work the same way. Using a System.out.println to display a message for the error condition is not ideal for a method like this. You can either ignore it or throw a runtime exception instead. Throwing an exception is probably the best option.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when are you updating the value of size?
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:And is setting the element to null the right thing to do? What would happen if you remove the last element, and then remove the last element again?


Correct me if I'm wrong but my understanding of arrays is when you have a capacity of 10 in a array but only have 5 values in the array then the remaining 5 values is null correct?

This is only one removal of the last value and not multiple, so it'll only be called once.

It should work for multiple times as long as I decremented the size. With that being said I should of done that.
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Lines 3 & 4 are redundant; if you remove them the program will work the same way. Using a System.out.println to display a message for the error condition is not ideal for a method like this. You can either ignore it or throw a runtime exception instead. Throwing an exception is probably the best option.


I was told not to mess with runtime exceptions until we cover them later in the semester, so that's why I didn't use that but to just get use to spotting them with a System.out.println for now.

Edit: I see what you mean about 3 & 4 because my "else" will do the same thing.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Correct me if I'm wrong but my understanding of arrays is when you have a capacity of 10 in a array but only have 5 values in the array then the remaining 5 values is null correct?



My question could have been worded better. I hadn't meant to imply that setting the values to null was incorrect, but rather only setting the values to null was incorrect. That being said your list would work fine without setting the values to null, although it is probably better to do that to stop references hanging around longer than they need to.


It should work for multiple times as long as I decremented the size. With that being said I should of done that.


Bingo, that's what I was hoping you'd see. Reducing the size is what removes an element from your list.
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using all the information I've learned over the past 10 minutes, here's another scenario.

What if I'm removing the second value of the array based list.



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse Schlicklin wrote:
What if I'm removing the second value of the array based list.


Think of a way to generalize that. Say you want to remove the Nth element of the list and there is no Nth element, what is the relationship between N and size? That's what you want to check. Otherwise you'll be like, "Well, what if I want to remove the third element? ... the fourth element?" and so on...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also confused as to why you are talking about arrays and ArrayList as if they are the same thing. As far as I can see, this topic is all about arrays.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I'm also confused as to why you are talking about arrays and ArrayList as if they are the same thing. As far as I can see, this topic is all about arrays.


Jesse Schlicklin wrote:We're given a array based list class and the list class has an array, capacity, and a size. We're assuming the size is always less than the capacity of the array and that size is the amount of values in the array based list.


Bear, I could be wrong but I don't think Jesse is talking about the java.util.ArrayList. Seems to me he's referring to a custom class that has the same name as the one in the standard library.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guess I'm confused. Carry on.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!