Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How display the first elements and last elements of collection of objects?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How display the first elements and last elements of collection of objects?

    For example we have the object created with the class that contain id, name and salary. The objects is posted by the salary and names if the salary is the same. What to do. We could not use indexes in arraylist. New for operator display only the whole set. But i need for example the 5 first element and 3 lasts?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    What class holds the collection of objects? What methods does that class have that would help you get the desired objects so you can display them?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    I think most fittable class in this situation is Arraylist. It has the method get(). But it is awkward to show every item. Especially the last ones. The last is possible if it possible to define the size of Array by the same name-size(). But do this method has it? This is very simple in standart array. Maybe transform the arraylist to simple array of objecs but how? For example I create the arraylist by such way Arraylist (<Object>?) workers=new Arqaylist .. Them add by:workers.add(1, "smith", 100)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    We could not use indexes in arraylist.
    Why not? Have you reviewed the ArrayList API for all that can be done?

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    Could you show how to do it in this collection Workers. It very simple as it is really to be? I surfed internet and say question about the first and the last one. And the answers were not so evident. Here the task is even comlex.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    about the first and the last one
    The index for the first one is 0 and the index for the last one is the size-1
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    Or it is very simple with indexes that begins at 0 and ends with i-1. So olny by one: system.out.println(workers.get(1).to sring). Or i am not correctu in displaying it?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    Sorry, I don't understand what the problem is.
    What happens when you compile and execute the code?
    If the code is getting errors, copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    I have not code. But you could make a simple tip as a Arraylist has a substrig(index1,index2) method to display or extract appropriate objects in the sequence. I this situation I have question what other collection is applicable to save the objects of such workers class with different types of variables-SET? LinkedList?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How display the first elements and last elements of collection of objects?

    What collection you use to save a list of objects would depend on how the code needs to access that list and on other characteristics of the data being saved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. drawString elements
    By KAJLogic in forum Java Theory & Questions
    Replies: 3
    Last Post: September 2nd, 2013, 06:27 PM
  2. REMOVE DUPLICATE ELEMENTS
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 10th, 2013, 09:24 AM
  3. How to check itersection of objects in on collection
    By Eldarmk2 in forum Java Theory & Questions
    Replies: 11
    Last Post: July 10th, 2012, 12:44 PM
  4. How to convert two elements into one
    By titowinky in forum Java Theory & Questions
    Replies: 4
    Last Post: May 8th, 2012, 07:43 PM
  5. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM