455,467 Members | 1,448 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

# Slicing Routine!

 P: n/a Hi All, I am looking for a smart solution to accomplish this task (.net 1.0) Appreciate your input I have a group of numbers in an arrayList 2,3,5,2,1,2,2 I need to output them into groups of 2,3,5 2 1 2 2 basically whenever I encounter 1 or 2 I bundle them up.. The numbers are in an arrayList, and I output them as string [] Arraylist Bundle(Arraylist) { // arr1 will contain (2,3,5) alReturn.Add(arr1); ................... return alReturn } TIA Jan 29 '07 #1
6 Replies

 P: n/a So whenever you get a 2 or a 1 you start a new bundle? Just if statement it then a bit like this, very rough code: foreach(int i in arr1) { if ( i>0 && i <= 2) { //check string array if its empty make a new one //if it isnt add it to array and start new one //add 1 or 2 to string array } else { //concat to string array all numbers after 1 or 2 } } return completeStringArray; "Vai2000"

 P: n/a Two approaches here, the first returns an arraylist of the starting points of each segment, and the second returns an arraylist of arraylists containing each segment. private void button1_Click(object sender, System.EventArgs e) { ArrayList a = new ArrayList(); int[] i = new int[] {2,3,5,2,1,2,2,3}; a.AddRange(i); ArrayList b = DoIt2(a); ArrayList c = DoIt(a); Console.WriteLine("."); } private ArrayList DoIt(ArrayList pList) { ArrayList a = new ArrayList(); for(int c=0;c

 P: n/a And here's the version without all the spurious extra code :) private ArrayList DoIt2(ArrayList pList) { ArrayList a = new ArrayList(); ArrayList b = null; for(int c=0;c

 P: n/a Anyone that names their method "DoIt" deserves respect. "DeveloperX" Two approaches here, the first returns an arraylist of the startingpoints of each segment, and the second returns an arraylist ofarraylists containing each segment.private void button1_Click(object sender, System.EventArgs e){ ArrayList a = new ArrayList(); int[] i = new int[] {2,3,5,2,1,2,2,3}; a.AddRange(i); ArrayList b = DoIt2(a); ArrayList c = DoIt(a); Console.WriteLine(".");}private ArrayList DoIt(ArrayList pList){ ArrayList a = new ArrayList(); for(int c=0;c

 P: n/a Sorry to discourage....but all the answers are spurious...! Input ArrayList: 2,3,4,20 1,2,2 2 2,2,2 1,2,3,4 1,1 null expected oututs Bundle Count=1 and the bundle will contain 2,3,4,20 Bundle Count=3 Bundle#1 wil contain 1 Bundle#2 will contain 2 and Bundle#3 will cotain 2 Bundle Count=1 Bundle#1 will contain 2 Bundle Count=3 Bundle#1 wil contain 2 Bundle#2 will contain 2 and Bundle#3 will cotain 2 Bundle Count=2 Bundle#1 wil contain 1 Bundle#2 will contain 2 ,3,4 {formatted with,} Bundle Count =2 Bundle#1 wil contain 1 Bundle#2 will contain 1 Null ............ "Daniel"

 P: n/a I also have JustDoIt for when it's really time critical ;) @Vai, I tested it with your set and a couple of others and it worked as expected. I did make a few assumptions like your string would start with a 1 or 2. Sorry it didn't help. On 29 Jan, 17:00, "Daniel"

### This discussion thread is closed

Replies have been disabled for this discussion.