Results 1 to 14 of 14
  1. #1
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default keep the first N elements of an array only

    What's the best way to keep only the first N element from an array ? Should I iterate it with a for loop, create a new array and overwrite it ?

    thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That works, if you want the first 3 elements only, create an array of 3 items and use a for loop to fill the small array with the large arrays first 3 values.

  3. #3
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    ya that works for sure, I was wondering if it is the optimal way to do it

  4. #4
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    I also have another question, should I load the entire text file into memory !?! I don't see other options at the moment.. or am I wrong ?

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

    Default

    Quote Originally Posted by aneuryzma View Post
    I also have another question, should I load the entire text file into memory !?! I don't see other options at the moment.. or am I wrong ?
    Nope, simply read the first N elements and forget about the rest (e.g. close the file and forget about it).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if you're looking for a one-liner, it is possible with java.util.ArrayList, if you'd store your values in an ArrayList rather than an array.

    here's the info:
    ----------------
    java.util.ArrayList

    protected void removeRange (int fromIndex, int toIndex)
    Removes the objects in the specified range from the start to the end index minus one.

    Parameters
    fromIndex the index at which to start removing.
    toIndex the index after the last element to remove.
    Last edited by ozzyman; 03-27-2011 at 02:09 PM.

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

    Default

    Quote Originally Posted by ozzyman View Post
    if you're looking for a one-liner, it is possible with java.util.ArrayList, if you'd store your values in an ArrayList rather than an array.

    here's the info:
    ----------------
    java.util.ArrayList

    protected void removeRange (int fromIndex, int toIndex)
    Removes the objects in the specified range from the start to the end index minus one.

    Parameters
    fromIndex the index at which to start removing.
    toIndex the index after the last element to remove.
    Nah, better use the public method subList( ... ) (and possibly a copy thereof if needed); that way you don't have to extend the ArrayList class because the removeRange( ... ) method is protected.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    What about getting the first N element from a hashList ?

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

    Default

    Quote Originally Posted by aneuryzma View Post
    What about getting the first N element from a hashList ?
    What is a hashLlist?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aneuryzma View Post
    What about getting the first N element from a hashList ?
    This makes no sense as HashSets and HashMaps (assuming you want one of these?) are not ordered. What are you trying to do here? What's the motivation behind these questions?

  11. #11
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    Sorry an ArrayList..

    the motivation is that I need to remove N random elements from a list.. I'm not giving you the big picture.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aneuryzma View Post
    Sorry an ArrayList..

    the motivation is that I need to remove N random elements from a list.. I'm not giving you the big picture.
    Consider attempting to solve your problems before asking them here, or if you've done that, showing us what you've tried, because that's how you'll learn.

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

    Default

    Quote Originally Posted by aneuryzma View Post
    Sorry an ArrayList..

    the motivation is that I need to remove N random elements from a list.. I'm not giving you the big picture.
    Have you read the replies in your own thread? Quite a few alternative solutions were mentioned. Also please read the documentation for the Collections framework.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    Sorry I was an hurry and I didn't pay attention. I've solved, thanks.

Similar Threads

  1. incrementing array elements
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 02:37 PM
  2. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 08:36 AM
  3. How to Insert elements into one array
    By zlich in forum New To Java
    Replies: 8
    Last Post: 01-02-2010, 02:37 PM
  4. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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