int[] myArray = { 8975, 8976, 8977, 8978, 8979, 8980, 8981, 8982, 8983, 8984, 8985, 8986, 8987, 8988, 8989, 8990, 8991, 8992, 8993, 8994, 8995, 8996, 8997, 8998, 8999, 9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007, 9008, 9009, 9010, 9011, 9012, 9013, 9014, 9015, 9016, 9017, 9018, 9019, 9020, 9021, 9022, 9023, 9024, 9025, 9026, 59478, 59479, 59480, 131856, 131857, 131858, 131859, 144530, 144531, 167284, 167285, 177557, 177901, 188274, 188275, 198014, 198015, 208720, 208738, 220295, 220437, 227920, 227921, 232515, 232516, 238561, 238562, 242575, 242576, 246619, 246620, 251758, 251759, 256516, 256517, 259966, 259967, 263476, 263477, 265455, 265456, 268904, 268905, 271804, 271805, 275017, 275018, 278414, 278415, 281569, 281570, 286318, 286319, 288535, 288536, 289256, 289257, 289480, 289481, 291119, 291120, 293771, 298412, 298413, 302086, 302087, 302758};
针对这个,怎么写一个类获得比如里面的具体数字的前一个和后一个数字呢?
比如最后第2个302087的前一个是302086,后一个是302758,对于最后1个数字302758前一个是302757,最后1个提示已经是最后1个了呢?
针对这个,怎么写一个类获得比如里面的具体数字的前一个和后一个数字呢?
比如最后第2个302087的前一个是302086,后一个是302758,对于最后1个数字302758前一个是302757,最后1个提示已经是最后1个了呢?
解决方案 »
- C#新建的cs怎么调用form1的timer控件
- SqlDataAdapter的update()方法为什么不能进行删除和更新???
- 请帮我看一下这个问题:用队列模拟理发店的排队情况(C#)
- 如何让子窗体真正包含孙窗体问题?
- 关于通用捕获异常的问题
- 我的问题很难熬!谁能弄出来?发扬精神啊!嘿嘿!
- ?---哈希表的东东....散分了.
- 能否在XSLT文件里将xml文件里的内容HtmlDecode化
- c#中这个System.Management命名空间怎么找不到啊,请大侠们帮帮忙!
- C#中有没有类似Template的特性,能模拟也行
- c#中如何通过点击一个窗口中的按钮去改变另一个窗体中控件的属性
- 用C#如何异步和服务器A通信(POST方式)
{
alert("the last one.");
}
{
//提示
}
else
{
myArray[Index-1];
myArray[Index+1];
}
int[] myArray = { 8975, 8976, 8977, 8978, 8979, 8980, 8981, 8982, 8983, 8984, 8985, 8986, 8987, 8988, 8989, 8990, 8991, 8992, 8993, 8994, 8995, 8996, 8997, 8998, 8999, 9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007, 9008, 9009, 9010, 9011, 9012, 9013, 9014, 9015, 9016, 9017, 9018, 9019, 9020, 9021, 9022, 9023, 9024, 9025, 9026, 59478, 59479, 59480, 131856, 131857, 131858, 131859, 144530, 144531, 167284, 167285, 177557, 177901, 188274, 188275, 198014, 198015, 208720, 208738, 220295, 220437, 227920, 227921, 232515, 232516, 238561, 238562, 242575, 242576, 246619, 246620, 251758, 251759, 256516, 256517, 259966, 259967, 263476, 263477, 265455, 265456, 268904, 268905, 271804, 271805, 275017, 275018, 278414, 278415, 281569, 281570, 286318, 286319, 288535, 288536, 289256, 289257, 289480, 289481, 291119, 291120, 293771, 298412, 298413, 302086, 302087, 302758};
int num = 302087;
int count = myArray.Length;
int no = -1;
int preNum = 0;
int nextNum = 0;
for(int i = 0; i < count; i ++)
{
int a = myArray[i];
if (a == num)
{
no = i;
break;
}
}if(no == -1)
{
//无该数
...
}
else if (no == 0)
{
//First
nextNum = myArray[no + 1];
}
else if (no == count - 1)
{
//Last
preNum = myArray[no - 1];
}
else
{
preNum = myArray[no - 1];
nextNum = myArray[no + 1];
}
{
int[] array = this.myArray;//数组myArray你已经在方法外部定义好!
int index = -1;
for(int i = 0;i < array.Length;i++)
{
if(num == array[i])
{
index = i;//记录匹配的数字下标
break;
}
}
if(index >= 0)
{
if(index == 0)
{
MessageBox.Show("参数" + num + "在第一个位置 - 没有前一位\n" + array[index].ToString() + " " + array[index + 1].ToString());
}
else if(index == array.Length - 1)
{
MessageBox.Show("参数" + num + "在最后一个位置 - 没有后一位\n" + array[index - 1].ToString() + " " + array[index].ToString());
}
else
{
MessageBox.Show(array[index - 1].ToString() + " " + array[index].ToString() + " " + array[index + 1].ToString());
}
}
else
{
MessageBox.Show("找不到与参数匹配的数字");
}
}大概就是这样!如果想更加人性化!那您只好在这段代码的基础上改良一下。。希望对您有帮助...
{
private int[] arrayContent=null;
private int currentIndex=0;
public ArrayOption(){}
public ArrayOption(int[] arrayContent){this.Init(arrayContent);} public void Init(int[] arrayContent){this.arrayContent=arrayContent;}
public void Previous()
{
if(currentIndex>-1)
currentIndex--
else
show("已经到顶");
} public void Next()
{
if(currentIndex<arrayContent.Length)
currentIndex++;
else
show("已经到底");
}
public bool ValiData()
{
if(arrayContent==null||arrayContent.Length<=0)
return false;
return true; public int getCurrentItem()
{
if(ValiData())
return arrayContent[currentIndex];
return -1;
}
}
this.FindNum(302087);
{
int[] myArray = { 8975, 8976, 8977, 8978, 8979, 8980, 8981, 8982, 8983, 8984, 8985, 8986, 8987, 8988, 8989, 8990, 8991, 8992, 8993, 8994, 8995, 8996, 8997, 8998, 8999, 9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007, 9008, 9009, 9010, 9011, 9012, 9013, 9014, 9015, 9016, 9017, 9018, 9019, 9020, 9021, 9022, 9023, 9024, 9025, 9026, 59478, 59479, 59480, 131856, 131857, 131858, 131859, 144530, 144531, 167284, 167285, 177557, 177901, 188274, 188275, 198014, 198015, 208720, 208738, 220295, 220437, 227920, 227921, 232515, 232516, 238561, 238562, 242575, 242576, 246619, 246620, 251758, 251759, 256516, 256517, 259966, 259967, 263476, 263477, 265455, 265456, 268904, 268905, 271804, 271805, 275017, 275018, 278414, 278415, 281569, 281570, 286318, 286319, 288535, 288536, 289256, 289257, 289480, 289481, 291119, 291120, 293771, 298412, 298413, 302086, 302087, 302758 };
string msg = string.Empty;
string preNum = string.Empty;
string nextNum = string.Empty;
if(getPreNext(myArray, 1, out preNum, out nextNum, out msg))
{
Console.WriteLine(msg);
Console.WriteLine("前一个:{0} 后一个:{1}", preNum, nextNum);
}
else
{
Console.WriteLine(msg);
}
Console.Read();
} static bool getPreNext(int[] myArr, int inNum, out string PreNum, out string NextNum, out string _MSG)
{
int iLength = myArr.Length;
if(inNum > iLength || inNum < 1)
{
PreNum = string.Empty;
NextNum = string.Empty;
_MSG = "输入的数组越界!";
return false;
} if(inNum == 1)
{
PreNum = "第一个了";
NextNum = myArr[inNum].ToString();
}
else if(inNum == iLength)
{
PreNum = myArr[inNum - 2].ToString();
NextNum = "最后一个了";
}
else
{
PreNum = myArr[inNum - 2].ToString();
NextNum = myArr[inNum].ToString();
}
_MSG = "获取数据成功!";
return true;
}
int length=a.Length;
int order=0;
bool exists = false;
foreach(int i in a)
{
order++;
if(i==2)
{
exists = true;
break;
}
}
if(exists && order == a.Length)
{
MessageBox.Show("最后一位数");
MessageBox.Show("前一位为"+a[order-1].ToString());
}
else if(exists && order == 0)
{
MessageBox.Show("第一位数");
MessageBox.Show("后一位为"+a[order+1].ToString());
}
else if(exists && order<a.Length && order>0)
{
MessageBox.Show("中间一位");
MessageBox.Show("前一位为"+a[order-1].ToString());
MessageBox.Show("后一位为"+a[order+1].ToString());
}
else
{
MessageBox.Show("没有找到");
}
{
public override string ToString()
{
return string.Format("Index:{0} Number:{1}", Index, myArray[Index]);
}
public int Index;
public MyNumber(int ANumber)
{
Index = -1;
for(int i = 0; i < myArray.Length; i++)
if (myArray[i] == ANumber)
{
Index = i;
break;
}
if (Index < 0)
throw new Exception("该数字不在列表中");
}
public MyNumber(short AIndex)
{
Index = AIndex;
}
public static int[] myArray = {
8975, 8976, 8977, 8978, 8979, 8980, 8981, 8982, 8983, 8984, 8985, 8986,
8987, 8988, 8989, 8990, 8991, 8992, 8993, 8994, 8995, 8996, 8997, 8998,
8999, 9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007, 9008, 9009, 9010,
9011, 9012, 9013, 9014, 9015, 9016, 9017, 9018, 9019, 9020, 9021, 9022,
9023, 9024, 9025, 9026, 59478, 59479, 59480, 131856, 131857, 131858,
131859, 144530, 144531, 167284, 167285, 177557, 177901, 188274, 188275,
198014, 198015, 208720, 208738, 220295, 220437, 227920, 227921, 232515,
232516, 238561, 238562, 242575, 242576, 246619, 246620, 251758, 251759,
256516, 256517, 259966, 259967, 263476, 263477, 265455, 265456, 268904,
268905, 271804, 271805, 275017, 275018, 278414, 278415, 281569, 281570,
286318, 286319, 288535, 288536, 289256, 289257, 289480, 289481, 291119,
291120, 293771, 298412, 298413, 302086, 302087, 302758 };
public MyNumber Next()
{
if (Index + 1 >= myArray.Length)
throw new Exception("没有下一个");
return new MyNumber((short)(Index + 1));
}
public MyNumber Prev()
{
if (Index - 1 < 0)
throw new Exception("没有上一个");
return new MyNumber((short)(Index - 1));
}
}
public interface IArray
{
public void GetIntValue(out int previousValue,out int nextValue);
}public class CommonArray:IArray
{ private int[] arrayContent=null;
private int currentIndex=0;
public CommonArray(){}
public CommonArray(int[] arrayContent){this.Init(arrayContent);} public void Init(int[] arrayContent){this.arrayContent=arrayContent;}
public void GetIntValue(int findContent,out int previousValue,out int nextValue)
{
previousValue=nextValue=-1;
for(int n=0;n<arrayContent.Length;n++)
{
if(arrayContent[n]==findContent)
{
previousValue=this.GetValue(n-1);
nextValue=this.GetValue(n+1);
}
}
} public int GetValue(index)
{
if(index>-1&&index<arrayContent.Length)
return arrayContent[index];
else
return -1;
}}
{
public void GetIntValue(int findContent,out int previousValue,out int nextValue);
}
接口写错了,改改
{
int[] loarr = { 8975, 8976, 8977, 8978, 8979, 8980, 8981, 8982, 8983, 8984, 8985, 8986, 8987, 8988, 8989, 8990, 8991, 8992, 8993, 8994, 8995, 8996, 8997, 8998, 8999, 9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007, 9008, 9009, 9010, 9011, 9012, 9013, 9014, 9015, 9016, 9017, 9018, 9019, 9020, 9021, 9022, 9023, 9024, 9025, 9026, 59478, 59479, 59480, 131856, 131857, 131858, 131859, 144530, 144531, 167284, 167285, 177557, 177901, 188274, 188275, 198014, 198015, 208720, 208738, 220295, 220437, 227920, 227921, 232515, 232516, 238561, 238562, 242575, 242576, 246619, 246620, 251758, 251759, 256516, 256517, 259966, 259967, 263476, 263477, 265455, 265456, 268904, 268905, 271804, 271805, 275017, 275018, 278414, 278415, 281569, 281570, 286318, 286319, 288535, 288536, 289256, 289257, 289480, 289481, 291119, 291120, 293771, 298412, 298413, 302086, 302087, 302758}; try
{
try
{
MessageBox.Show("The Current:" + loarr[int.Parse(txtbx_arrindex.Text)].ToString());
}
catch(IndexOutOfRangeException ex)
{
MessageBox.Show(ex.Message);
return;
} try
{
MessageBox.Show("Previous:" + loarr[int.Parse(txtbx_arrindex.Text) - 1].ToString());
}
catch//(IndexOutOfRangeException ex)
{
MessageBox.Show("The current is the first one!");
} try
{
MessageBox.Show("Next:" + loarr[int.Parse(txtbx_arrindex.Text) + 1].ToString());
}
catch//(IndexOutOfRangeException ex)
{
MessageBox.Show("The current is the last one!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
{
Hashtable table = new Hashtable();
for(int i=0;i<myArray.Length;i++)
{
table.Add(myArray[i],i);
}
if(table.Contains(val))
{
int index=(int)table[val];
if(index==myArray.Length)
{
next=-1;
pre = myArray[index-1];
}
else if(index==0)
{
pre = -1;
next = myArray[index+1];
}
else
{
pre = myArray[index-1];
next = myArray[index+1];
}
}
}
MS 已经结贴了