Results 1 to 12 of 12
- 06-08-2011, 08:33 AM #1
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
How to selection the elements in an ArrayList?
Hi...
I make an arrayList to store my objects. And I want every time the user will put the new object, the program will selection the arrayList first. It will see whether the new objects are same with the object inside the arrayList or not. If it is same then that object will not be put into the arrayList and there will be a message. So every time the user press the submit button, it will make selection first. I also save that arrayList in file.
How to make it ?
- 06-08-2011, 08:45 AM #2
- 06-08-2011, 08:51 AM #3
Use a Set instead as no duplicates are allowed.
- 06-08-2011, 09:12 AM #4
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
No...
.gif)
I already try to make it...
Here is the code that I wrote...
ArrayList<Part> p = new ArrayList<Part>();
Data_File.read_object(p);
.
.
.
public boolean addParts(Object... objPris)
{
while(i < p.size()){
if(p.get(i).getId() == (String)objPris[2])
return false;
i++;
}
p.add(p1);
Data_File.write_object(p);
return true;
In the JFrame In the ActionPerformed(Action event e) ...
I write:
if(prisController.addParts(objPris))
JOptionPane.showMessageDialog(null, "Added Successfully");
else
JOptionPane.showMessageDialog(null, "ID have been assigned, Please change the ID");
But the result is always put the objects eventhough it's same ...
Thank you
- 06-08-2011, 09:28 AM #5
Use equals method if you really want to compare Strings.if(p.get(i).getId() == (String)objPris[2])
Anything after return is unreachable.return false;
i++;
Is the entire code too big to post? This half code is confusing. And use code tags next time while posting code.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-08-2011, 09:37 AM #6
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
Use .equals() method instead of ==. The == looks if it refrences the same memory location while .equals() will compare value equality.
- 06-08-2011, 10:13 AM #7
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
[QUOTE=goldest;213967]Use equals method if you really want to compare Strings.
This is the solution, I just need to delete one 'equal' and my problem is solved :D
Thank you very much :)
Sorry, this is my first time to post with code.gif)
*Thank you all for answering my question-.gif)
- 06-08-2011, 10:58 AM #8
It's all right. Glad to know that you resolved your problem.
Come back again if you face any problems and the people here would be more than happy to help you out..gif)
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-08-2011, 12:33 PM #9
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Thank you

Nice to meet you
- 06-08-2011, 01:24 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Allow me to repeat Junky's reply #3:
kind regards,
Originally Posted by Junky
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-09-2011, 12:42 AM #11
Thanks Jos
I was beginning to think I was invisible too!
- 06-09-2011, 01:28 AM #12
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
extracting 5 random elements from an ArrayList
By ankit1801 in forum New To JavaReplies: 3Last Post: 04-13-2011, 06:17 AM -
Adding elements to an ArrayList
By ArcherSam in forum Advanced JavaReplies: 7Last Post: 01-28-2011, 03:05 PM -
Problem in adding arrayList elements
By cool in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:27 PM -
Cannot swap elements in ArrayList
By glchau in forum New To JavaReplies: 4Last Post: 05-30-2010, 08:03 AM -
Accessing elements of an ArrayList which contains 2D arrays
By Willi in forum New To JavaReplies: 5Last Post: 01-18-2010, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks