java


Preventing repetitive items from being printed [duplicate]


This question already has an answer here:
Preventing multiple objects from being printed
1 answer
How do I check to see if an item is already in a list and prevent it from being added.
Here is what I have so far:
public static void main(String[] args)
{
//Before checkout
Item list[] = new Item[10];
for(int i = 0; i < list.length; i++)
{
int random = (int)(Math.random() * 2);
if(random == 0)
list[i] = new Perish();
else
list[i] = new Nonperish();
}
for(int i = 0; i < list.length; i++)
{
for(int j = 1; j < list.length; j++)
if(list[i].getName().equals(list[j].getName()))
{
int newRandom = (int)((Math.random() * 2));
if(newRandom == 0)
list[i] = new Perish();
else
list[i] = new Nonperish();
}
}
Inspite using an array try using HashSet. To make the object Item work in a HashSet you should override hasCode() and equals() methods. I assume that getName() returns String so these methods should be like this:
#Override
public int hashCode() {
return getName().hashCode();
}
#Override
public boolean equals(Object o) {
if(o == null){
return false;
}
if(!o.getClass().equals(getClass())){
return false;
}
return this.getName().equals(((Item)o).getName());
}
And then try using this code. At the end your array can't have any repeated item:
Item list[] = new Item[10];
Set<Item> itemSet = new HashSet();
for(int i = 0; i < list.length; i++)
{
do{
int random = (int)(Math.random() * 2);
if(random == 0)
list[i] = new Perish();
else
list[i] = new Nonperish();
if(itemSet.contains(list[i])){
continue;
}else {
itemSet.add(list[i]);
break;
}
}while(true);
}

Related Links

while after converting string to int
Can I convert List<V> to Map<K, List<V>> without foreach?
Remove unique values from array
How to add logout functionality in spring security [closed]
Android Google Maps - NullPointerException - Location.getLatitude()
Issues with algorithm to merge adjacent rectangles
why I cannot use override when implement interface? [duplicate]
stack over flow error in generic class [duplicate]
how to determine pattern of cards in 3 Card Game to Find Winner?
custom exception only prints out null when called in a try/catch block
Appending words to sentence
Preferred method of configuration for App Engine Standard
add JSONOnject at the beggining of my old file JSON jackson java
How to hide button open first screen to second screen in android?
How to take parameter from text box as a form of date
What is the difference between the Java awt package, Javafx, and Java Swing [closed]

Categories

HOME
listview
urbancode
google-play
android-4.4-kitkat
google-oauth
maven-3
paw-app
amortized-analysis
survey
sql-server-2016
blueprintjs
virtualization
vifm
quicklook
dtrace
try-catch
serilog
oxyplot
openedx
autoconf
extjs5
wijmo
emgucv
opennlp
centos6.5
fgetcsv
tooltipster
elasticsearch-ruby
msys2
large-file-upload
jaxb2
vision
subdomains
twilio-api
oracle-fusion-middleware
copying
usbserial
mapdb
hockeyapp
mixture-model
hue
user-controls
ncalc
janrain
vao
modelmapper
flashair
acoustics
google-datalayer
flickr-api
zip4j
directory-structure
sybase-asa
windows-mobile-6.5
python-cryptography
minimization
parallel-data-warehouse
xcb
firmata
spring-android
vmware-tools
fody
pcf
player
fputcsv
persist
sigabrt
bluemix-app-scan
independentsoft
xpath-1.0
ford-fulkerson
rtbkit
spim
execute
metaclass
jmeter-maven-plugin
ionic
mono-embedding
mmc
proj4js
bigbluebutton
tt-news
rdtsc
internet-connection
google-reader
valuechangelistener
poller
braille
ember-charts
meteor-velocity
quartz-graphics
flashvars
angularjs-controller
osql
self-extracting
inotifycollectionchanged
paster
preference
nsviewanimation
swing-app-framework
multiple-languages
lzh
uiq3

Resources

Encrypt Message