By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ 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
Share this Question
Share on Google+
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" <no****@microsoft.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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 #2

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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;
}
private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}

On 29 Jan, 16:12, "Vai2000" <nos...@microsoft.comwrote:
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 #3

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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}
On 29 Jan, 16:49, "DeveloperX" <nntp...@operamail.comwrote:
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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;

}On 29 Jan, 16:12, "Vai2000" <nos...@microsoft.comwrote:
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- Hide quoted text -- Show quoted text -
Jan 29 '07 #4

P: n/a
Anyone that names their method "DoIt" deserves respect.
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}
On 29 Jan, 16:49, "DeveloperX" <nntp...@operamail.comwrote:
>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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;

}On 29 Jan, 16:12, "Vai2000" <nos...@microsoft.comwrote:
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- Hide quoted text -- Show quoted text -

Jan 29 '07 #5

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" <no****@pokercat.co.ukwrote in message
news:OH**************@TK2MSFTNGP04.phx.gbl...
Anyone that names their method "DoIt" deserves respect.
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}
On 29 Jan, 16:49, "DeveloperX" <nntp...@operamail.comwrote:
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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;

}On 29 Jan, 16:12, "Vai2000" <nos...@microsoft.comwrote:

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- Hide quoted text -- Show quoted text -


Jan 29 '07 #6

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" <nos...@pokercat.co.ukwrote:
Anyone that names their method "DoIt" deserves respect.

"DeveloperX" <nntp...@operamail.comwrote in messagenews:11**********************@a34g2000cwb.g ooglegroups.com...
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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
b = new ArrayList();
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}
On 29 Jan, 16:49, "DeveloperX" <nntp...@operamail.comwrote:
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<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
a.Add(c);
}
}
return a;}private ArrayList DoIt2(ArrayList pList)
{
ArrayList a = new ArrayList();
ArrayList b = null;
for(int c=0;c<pList.Count;c++)
{
if(1 == (int)pList[c] || 2 == (int)pList[c])
{
if(null == b)
{
b = new ArrayList();
}
else
{
b = new ArrayList();
}
a.Add(b);
}
b.Add(pList[c]);
}
return a;
}On 29 Jan, 16:12, "Vai2000" <nos...@microsoft.comwrote:
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- Hide quoted text -- Show quoted text -
Jan 29 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.