给出一组int型数组如下(长度已知)
0 0 0 …… 0 2 4 5 6 8 2 …… 4 0 0 0…… 0 4 3 2 …… 3 0 0 0 ……
数组的特征是每隔若干个0后就有若干个自然数 ,现在把0看成空值,现在要实现一个功能,将这个数组里的0全部剔除,将自然数保存在另外定义的数组中,我表达能力不好,举个例子说明下吧如下整型数组 a[10] {0 0 1 2 3 0 0 0 5 8}
我要实现的功能是处理后分成两个数组 b[3] {1 2 3} 和c[2] {5 8}
同理如果数组是 {0 0 1 2 0 0 3 4 0 0 5}这样就分成3个数组 {1 2}{3 4}{5}
原始数组长度是已知的,但是不知道里面有多少个 0,也就是空值,也有可能整个数组全是0或全不是。我不知道说明白了没有,请大家帮忙,多谢
0 0 0 …… 0 2 4 5 6 8 2 …… 4 0 0 0…… 0 4 3 2 …… 3 0 0 0 ……
数组的特征是每隔若干个0后就有若干个自然数 ,现在把0看成空值,现在要实现一个功能,将这个数组里的0全部剔除,将自然数保存在另外定义的数组中,我表达能力不好,举个例子说明下吧如下整型数组 a[10] {0 0 1 2 3 0 0 0 5 8}
我要实现的功能是处理后分成两个数组 b[3] {1 2 3} 和c[2] {5 8}
同理如果数组是 {0 0 1 2 0 0 3 4 0 0 5}这样就分成3个数组 {1 2}{3 4}{5}
原始数组长度是已知的,但是不知道里面有多少个 0,也就是空值,也有可能整个数组全是0或全不是。我不知道说明白了没有,请大家帮忙,多谢
解决方案 »
- [求]设置word“密码保护”和“标记为最终状态”的C#代码
- 用c#来获取局域网内的所有ip地址,计算机名称,监控每个ip网络流量,急急急!!!
- 使用事务执行一个新增,加for循环多个更新,提交时提示:此SqlTransaction已完成,它再也无法使用
- [请教]关于项目引用的问题
- c#运行flash游戏的问题
- 如何往本地写一个excel文件,而且不需要用户按保存按钮(在线等待)
- 求教!!ListView中动态增加列,在线等
- 3des加密算法的问题,急
- 关于C#的运行问题!
- 谁有使.Net可以连接到MySQL数据库的 MySQLDriverCS ?
- word保存进数据库问题
- 键盘敲击声音如何取消?
朋友 看看这个问题。http://community.csdn.net/Expert/topic/5686/5686047.xml?temp=.8212702
.net 就好了,可以充分利用 ArrayList/List<T> 了, 不用动态维护数组长度了
-------------------------------------------
List<T>比ArrayList要科学,ArrayList分配空间是自动的,但他是2、4、8、16、32、...这样动态来分配的
{
int[] a = new int[] { 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 5 };
List<int[]> results = new List<int[]>();
int index = 0;
while (index < a.Length)
{
while (index < a.Length && a[index] == 0)
{
index++;
}
if (index < a.Length)
{
List<int> newarray = new List<int>();
while (index < a.Length&&a[index] != 0)
{
newarray.Add(a[index]);
index++;
}
results.Add(newarray.ToArray());
}
}
foreach (int[] ia in results)
{
Console.Write("{");
foreach (int var in ia)
{
Console.Write(var);
Console.Write(" ");
}
Console.WriteLine("}");
}
}
int[] test = { 0,1,2,0,0,3,0,5,1,8,7,0,0,0,0,5,9,4};
int length = test.Length;
int[][] need = new int[length][];
for (int i = 0; i < length; i++)
{
//具体去比较实现
}
string s = "002323001230000";
string[] ss = s.Split('0');
foreach (string s in ss)
{
if (s != null && s != string.Empty)
{
list.Add(s);
}
}ss = list.ToArray();
return ss;
--------------------------------
Macosx(咋就不结贴呢)
好!
const int separator = 0;
for (int i = 0; i < source.Length; i++) {
if (source[i] != separator) {
if (i == 0 || source[i-1] == 0) {
result.Add(new List<int>());
}
result[result.Count - 1].Add(source[i]);
}
}
string s = "002323001230000";
string[] ss = s.Split('0');
foreach (string s in ss)
{
if (s != null && s != string.Empty)
{
char[] ss1 = s.ToCharArray();
list.Add(ss1);
}
}ss = s.ToCharArray();
return ss;
string s = "002323001230000";
string[] ss = s.Split('0');
foreach (string s in ss)
{
if (s != null && s != string.Empty)
{
char[] ss1 = s.ToCharArray();
list.Add(ss1);
}
}ss = s.ToCharArray();
return ss;
的方法最好,也最简单。
List<List<int>> result = new List<List<int>>();
const int separator = 0;
for (int i = 0; i < source.Length; i++) {
if (source[i] != separator) {
if (i == 0 || source[i-1] == 0) {
result.Add(new List<int>());
}
result[result.Count - 1].Add(source[i]);
}
} List<int[]> dst = new List<int[]>(result.Count);
foreach (List<int> a in result) {
dst.Add(a.ToArray());
}
// out
StringBuilder sb = new StringBuilder();
foreach (int[] a in dst) {
foreach (int n in a) {
sb.Append(n).Append(",");
}
sb.AppendLine();
}
HtmlPreWriter.Flush(sb);
/*
1,2,3,
5,8,
11,55,
8,9,10,
*/
List<int> list = new List<int>();
foreach (int i in ary)
{
if (i>0)
list.Add(i);
}
// 这个list里面就是要的所有元素
的方法很好了,关键就是用好split()方法了
Array ary; // 原数组
List<List<int>> list = new List<List<int>>();
List<int> t = new List<int>();
int buff = 0;
foreach (int i in ary)
{
if (i>0)
{
t.Add(i);
}
else if (i==0 && buff>0)
{
list.Add(t);
t.Clear();
}
buff = i;
}
return list;// 如果要取数据,使用list[i][j]来取
// i表示取第几个数组,j表示取这个数组的第几个元素
int[] ints = { 0, 1, 2, 123, 0, 0, 0, 34, 16, 0, 7 };
foreach (int i in ints)
{
sb.Append(i.ToString());
sb.Append(',');
}List<int[]> list = new List<int[]>();
string s = sb.ToString();
string[] ss = s.Split('0');
foreach (string s2 in ss)
{
if (s2 != null && s2 != string.Empty)
{
string[] ss2 = s2.Split(',');
List<int> ints2 = new List<int>();
foreach (string s3 in ss2)
{
if (s3 != null && s3 != string.Empty)
{
ints2.Add(Convert.ToInt32(s3));
}
}
if (ints2.Count != 0)
{
list.Add(ints2.ToArray());
}
}
}
return list.ToArray();
intArr.Initialize();
intArr[4] = 4; intArr[5] = 5; intArr[6] = 6; intArr[80] = 80; intArr[83] = 83; intArr[84] = 84; intArr[98] = 98; intArr[99] = 99;
List<List<int>> ls = new List<List<int>>();
List<int> list = new List<int>();
for (int i = 0; i < intArr.Length; i++)
{
if (i == intArr.Length - 1)
{
if (intArr[i] != 0)
list.Add(intArr[i]);
if(list.Count !=0)
ls.Add(list);
break;
}
if (intArr[i] != 0)
{
list.Add(intArr[i]);
}
else
{
if (list.Count != 0)
{
ls.Add(list);
list = new List<int>();
}
continue;
}
}
Response.Write(ls.Count); =4-----------------
测试过
List<int> tmpList = new List<int>();
int counter = 0;
int[] a = new int[] { 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 5 };
string input = string.Empty;
foreach (int i in a)
{
input += i.ToString();
}
string[] pieces = Regex.Split(input, @"0+");
foreach (string piece in pieces)
{
if (piece != "")
{
counter++;
foreach (char c in piece)
{
tmpList.Add(int.Parse(c.ToString()));
}
result[counter] = tmpList.ToArray();
tmpList = new List<int>();
}
}
----^^^^
数据都有了。改成数组不难吧应该,这又不是考试,呵呵就用list或者Dictionary啦。多好用的东西
正解,通过测试!
{
int i = 0;
int j = 0;
List<int[]> arrlist = new List<int[]>();
if (arr == null || arr.Length == 0)
return null;
for (; i < arr.Length;i++)
{
if (i == 0 || (arr[i]!=0 && arr[i - 1] == 0) )
{
j = i;
continue;
}
if (arr[i] == 0 && arr[i-1] != 0)
{
int[] tmparr = new int[i - j];
for (int k = 0; k < i-j; k++)
{
tmparr[k] = arr[k + j];
}
arrlist.Add(tmparr);
}
}
if (arrlist.Count == 0)
return null;
else
return arrlist.ToArray();
}