• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to avoid duplicate data in arraylist?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm creating sales application using java and mysql. and now I have some trouble on my codes.
By the way, here is the form :
https://drive.google.com/file/d/0B-CpAcAUqtVkSXl4Tzl3VjFCS2s/view

when I click the small button below 'Kode Barang'. (...) , I can choose any item I want to purchase, after clicked the item then it shows the name of the item and price automatically. then I input the amount of the item I want to buy, then click on the '+' button to add item so the item will be show in the table below. and now here is the question : how to avoid duplicate item if I clicked the same item again? e.g if i already clicked the item 1, then when I clicked the item 1 again, it shows some message like "This item is already picked" or else. So I just can choose one item for one time. can you help me guys? I'm so sorry if my english is bad.
Here is the code (form) :




 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sally Wilton wrote:how to avoid duplicate item if I clicked the same item again? e.g if i already clicked the item 1, then when I clicked the item 1 again, it shows some message like "This item is already picked" or else.

Whenever you store those picked items, upon each item pick you check if it is not stored yet, if it is, show message you want.

Part of the problem with your code is, that it is not readable, and it is for a simple reason, too much code in one method and all your code structure is very deeplly nested. Try to refactor it by decomposing to more smaller methods, might the answer will become even clear to yourself.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are many possibilities:-
You can try iterating the List to see whether you have already added the element, but as the List becomes larger, performance will become slower.
You can try having a Set in parallel with the List: if you can add the element to the Set, add it to the List too.
There is a Set implementation which has a List as well, so you might be able to use that instead.
You can put all your items in a Set and remove them when they are selected.
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid creating dynamic SQL strings like this:

This kind of code opens you up to SQL Injection attacks and is a big security risk / vulnerability. Use parameterized queries instead.
 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note on posting: you can't use a Google Drive image with the [img] tags.  Use the [url] tags or just a bare URL and the system will turn it into a link.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!