首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

,高手留步1分钟,顶者有分

2011-12-17 
十万火急,高手留步1分钟,顶者有分~int[]myArray{8975,8976,8977,8978,8979,8980,8981,8982,8983,8984,898

十万火急,高手留步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};
针对这个,怎么写一个类获得比如里面的具体数字的前一个和后一个数字呢?
比如最后第2个302087的前一个是302086,后一个是302758,对于最后1个数字302758前一个是302757,最后1个提示已经是最后1个了呢?

[解决办法]
拿 "302087 "来说,先找出其在数组中的索引.然后索引减一,取出302086,索引加一,取出302758

在索引加减的时候,要判断是否越界,是就提示: "最后1个了 ", "第一个了. "
[解决办法]
拿 "302087 "来说,先找出其在数组中的索引.然后索引减一,取出302086,索引加一,取出302758

在索引加减的时候,要判断是否越界,是就提示: "最后1个了 ", "第一个了. "
======================================================================

[解决办法]
myArray[i]

if (i==myArray.Length-1)
{
alert( "the last one. ");
}
[解决办法]
顶你!!
[解决办法]
int[] myArray = { 8975, 8976, 8977} 先把这个按原有需求解决了 , 再回头考虑效率就OK啦
[解决办法]
直接取【数组下标+】与【数组下标-1】当发上异常数组越界异常时System.IndexOutOfRangeException,进行判断处理。
[解决办法]
不明白什么意思,顶你下吧
[解决办法]
循环求下标,或转为ArrayList,有个IndexOf方法得下标
[解决办法]
楼主的思路不好
[解决办法]
if(Index < 0 || Index > myArray.Length)
{
//提示
}
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];
}

[解决办法]
顶你下吧
[解决办法]
private void FindNum(int num)
{
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( "找不到与参数匹配的数字 ");
}
}

大概就是这样!如果想更加人性化!那您只好在这段代码的基础上改良一下。。

希望对您有帮助...
[解决办法]
static void Main(string[] args)
{
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[] a = {1,2,3};
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 class MyNumber
{
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;
}

}

热点排行