Results 1 to 12 of 12
  1. #1
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default 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 ?

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by MarMer View Post
    How to make it ?
    How are you making it at the moment? Or you want us to make it for you?

    Anything attempted from your side?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Use a Set instead as no duplicates are allowed.

  4. #4
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    How are you making it at the moment? Or you want us to make it for you?

    Anything attempted from your side?

    Goldest
    No...
    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

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    if(p.get(i).getId() == (String)objPris[2])
    Use equals method if you really want to compare Strings.

    return false;
    i++;
    Anything after return is unreachable.

    Is the entire code too big to post? This half code is confusing. And use code tags next time while posting code.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    Use .equals() method instead of ==. The == looks if it refrences the same memory location while .equals() will compare value equality.

  7. #7
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    [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


    *Thank you all for answering my question

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Thank you
    Nice to meet you

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Allow me to repeat Junky's reply #3:

    Quote Originally Posted by Junky
    Use a Set instead as no duplicates are allowed.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Thanks Jos

    I was beginning to think I was invisible too!

  12. #12
    MarMer is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Thanks Jos

    I was beginning to think I was invisible too!
    Thanks to you and to Jos who were willing to help me
    Thank you for replied me

    Nice to meet you all

Similar Threads

  1. extracting 5 random elements from an ArrayList
    By ankit1801 in forum New To Java
    Replies: 3
    Last Post: 04-13-2011, 06:17 AM
  2. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  3. Problem in adding arrayList elements
    By cool in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:27 PM
  4. Cannot swap elements in ArrayList
    By glchau in forum New To Java
    Replies: 4
    Last Post: 05-30-2010, 08:03 AM
  5. Replies: 5
    Last Post: 01-18-2010, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •