Results 1 to 10 of 10
  1. #1
    kronos is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Question Deal with arraylist

    I have an arraylist of this number:

    [0,1,2,3,4,5,15,16,17,18,19,20]

    How can I, make the overall number at above into range and store it inside new arraylist like below:

    [0-5,15-20]

    Is there any solution.... I'm stuck here.... Thank you in advance...

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

    Default Re: Deal with arraylist

    Quote Originally Posted by kronos View Post
    I have an arraylist of this number:

    [0,1,2,3,4,5,15,16,17,18,19,20]

    How can I, make the overall number at above into range and store it inside new arraylist like below:

    [0-5,15-20]

    Is there any solution.... I'm stuck here.... Thank you in advance...
    This doesn't make sense. If the ArrayList holds Integers there is no way to do this since 0-5 is meaningless as an Integer. I think that you have a basic confusion of a number vs the String representation of a number. How about instead of trying to bastardize ArrayList, change how its values are represented to the user. Perhaps what you want is to create a method that takes as a single parameter a List<Integer> and returns a String representation of what you are desiring, or even a List<String> of what you're trying to achieve. This shouldn't be too hard to do, and I invite you to try to attempt this yourself first and show us what you can come up with.

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

    Default Re: Deal with arraylist

    Quote Originally Posted by kronos
    ArrayList<String> number_list = new ArrayList<>();//all number inside here [0,1,2,3,4,5,20,21,22,23,24,25]
    ArrayList<String> view_list = new ArrayList<>();//inside here will be the output like this [0-5,20-25]

    for(int c=0; c<number_list.size(); c++)
    {
    if((Integer.parseInt(number_list.get(c+1)))-(Integer.parseInt(number_list.get(c)))==1)
    {

    }
    }

    Is it correct.....?
    1. To see if code is correct, test it. Does it compile? Does it behave correctly? You should be able to answer that yourself.
    2. For more help, please do not private message. Post your code attempt with code tags as a reply to your question. In this reply tell us if the code works or not and ask as specific as possible questions about your code and your problem.

  4. #4
    kronos is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Deal with arraylist

    The code is correct....
    But I don't have idea to make it ... I need help...
    From this form : [0,1,2,3,4,5,15,16,17,18,19,20] //number arraylist
    Into this form : [0-5,15-20] //view arraylist
    Do u have any suggestion... Im using String arraylist and going to store back in new String arraylist...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Deal with arraylist

    Well, I would start by writing a loop that iterates over the List elements storing a value in a variable declared before the start of the loop. Then I would be able to compare the current element with the last element and build a String (probably using StringBuffer / StringBuilder) with the desired representation.

    Of course, this assumes that the List is already sorted; if it isn't, there's a method in the COllections API for that ... unless for this homework you have to do-it-yourself.

    Now lets see your efforts in that direction.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    kronos is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Deal with arraylist

    Quote Originally Posted by DarrylBurke View Post
    Well, I would start by writing a loop that iterates over the List elements storing a value in a variable declared before the start of the loop. Then I would be able to compare the current element with the last element and build a String (probably using StringBuffer / StringBuilder) with the desired representation.

    Of course, this assumes that the List is already sorted; if it isn't, there's a method in the COllections API for that ... unless for this homework you have to do-it-yourself.

    Now lets see your efforts in that direction.

    db
    I know how to do the looping of each of the arraylist item...
    My problem is what is the logic...?
    Any clue bro...
    Its urgent....

  7. #7
    kronos is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Deal with arraylist

    Sorry bro...
    I did not get your idea...
    Actually i have this number arraylist: [0,1,2,3,4,5,15,16,17,18,19,20]
    You said need to loop the arraylist and do comparison and store it in StringBuffer...
    How it could happen..?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Deal with arraylist

    kronos, please ask technical questions in the forum and not by PM. I have deleted the message you sent me. Don't harass other members with your PMs or you may face a ban.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Deal with arraylist

    Quote Originally Posted by kronos View Post
    My problem is what is the logic...?
    Play in your head what the loop needs to do, or write it out in notepad or something. Then try to get that behavior in your code

    1: NEW RANGE: range_start = 1, range_end = 1
    2: range_end = 2
    3: range_end = 3
    4: range_end = 4
    5: range_end = 5
    15: oops, that is higher as 5+1. addRangeToList(range_start, range_end). NEW RANGE, range_start = 15, range_end = 15
    16: range_end = 16
    etc.



    Any clue bro...
    Pro tip: don't use slang.

    Its urgent....
    Pro tip: and certainly don't use urgency as an argument or you'll get LESS help. This reads like "I'm in a hurry, move your ass and spell it out for me".

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Deal with arraylist

    Thread moved from AWT/Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How do I deal with this error
    By Publiccarbus in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 04:17 AM
  2. Replies: 1
    Last Post: 08-07-2007, 05:37 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
  •