1. Member
Join Date
Jul 2013
Posts
4
Rep Power
0

## 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. ## Re: Deal with arraylist

Originally Posted by kronos
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. ## Re: Deal with arraylist

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.

4. Member
Join Date
Jul 2013
Posts
4
Rep Power
0

## 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. ## 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

6. Member
Join Date
Jul 2013
Posts
4
Rep Power
0

## Re: Deal with arraylist

Originally Posted by DarrylBurke
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. Member
Join Date
Jul 2013
Posts
4
Rep Power
0

## 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. ## 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

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
10

## Re: Deal with arraylist

Originally Posted by kronos
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. ## Re: Deal with arraylist

db

#### Posting Permissions

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