• Post Reply Bookmark Topic Watch Topic
  • New Topic

indexing  RSS feed

 
Gary Doyle
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Does anyone know you can change this code so that when a lot is removed it wont move up in the index. e.g if you removed lot 2, lot 3 would move into lot 2.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of an ArrayList you probably want to use a HashMap. With a Map you can use lot-number as the key to look up any entry. Deleting the entry has no effect on other entry's keys.
 
Gary Doyle
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll have a look into that thanks, I just couldn't find anything anywhere to help
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why you'd even care what position in the list any lot is in. If you're using Java 8, you could just filter, find the first one, then use an Optional<Lot>:

You can even get rid of the if statement:

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Doyle wrote:Does anyone know you can change this code so that when a lot is removed it wont move up in the index. e.g if you removed lot 2, lot 3 would move into lot 2.

So some smells here:
1. Why use a List if the lot's number field has to be in synch with its relative position? You should use a fixed structure, like a Lot[] (an array) then.
2. If you want a Lot's relative position to be fixed, what's the point of having a lot.number field then?

The cognitive dissonance in these design choices is what is making it difficult for you to understand how to write your code.

If, indeed, the lot's relative position within a collection is not really important after all, then you can use a Map as suggested. The other option is to use the Stream API as I showed.
 
Gary Doyle
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is code giving to us from the lecturer, he asked us to change it so that it wouldn't move up the index as a excercise. None of this is my code and from what I've found myself is that it is easier to delete all the code and start again like you have. Thanks for your help
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Doyle wrote:it is code giving to us from the lecturer, he asked us to change it so that it wouldn't move up the index as a excercise. None of this is my code and from what I've found myself is that it is easier to delete all the code and start again like you have. Thanks for your help

If you don't want to have it move up an index, then instead of deleting it replace it with null using:
add(int index, E element)
This will, of course, result in more and more null entries over time and is not an ideal choice, but it works.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Doyle wrote:it is code giving to us from the lecturer

Hmm. Well, since it is for a class then I really shouldn't have given a full solution like that. Cat's out of the bag now though. I guess since the solution given doesn't meet the requirements, no harm no foul then. At any rate, you should consider Carey's suggestion and go through the JavaDocs for List and see if you can find an appropriate method that you can call to effectively delete an item from the list without actually removing an item and shifting everything around. You should be able to find something that fits the bill.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!