在for循环中读取数组中的第二个值 [英] read second value in array in for loop

查看:98
本文介绍了在for循环中读取数组中的第二个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我读一下数组中的第二个值

i意思是:

int [] marks = new int [5] {99,98,92,97 ,95};

int [] marks1 = new int [5] {13,19,5,4,2};

int [] marks2 = new int [ 5] {9,1,17,288,14};

我想要读取这些数组中的第二个值:98,19,1

并将其放入一个新阵列我怎么能这样做? for for循环我的意思是

谢谢。

解决方案

在我看来,从数组中获取值将是一个非常好的主意,像这样,



  int  [] arr = {marks [  1 ],marks1 [ 1 ],marks2 [ 1 ]}; 





够了。它将具有3个元素的数组,在这三个数组中具有第二个元素的值。



否则,如果你想使用循环获取值。您可以编写循环并使用索引变量获取值。但是,记住,你只需要一个值所以不要在额外循环上浪费CPU周期。这样写,



  int  a =  0 ; 
for int i = 1 ; i < 2 ; i ++){a = marks [i]; }





上面的循环只运行一次,因为在第一次条件为真时,那么它将是假的,所以在得到索引1(元素2)的值会破坏。这样写,



  int  [] arr = {< span class =code-digit> 0 , 0  0 };  //  任意数据(不需要) 

for int i = 1 ; i < 2 ; i ++){arr [ 0 ] =标记[ 1 ]; }
for int i = 1 ; i < 2 ; i ++){arr [ 1 ] = marks1 [ 1 ]; }
for int i = 1 ; i < 2 ; i ++){arr [ 2 ] = marks2 [ 1 ]; }

// 可能的值是
{ 98 19 1 }





你去。


为什么你需要一个for循环?

  var  result =  new  [] {m​​arks [ 1 ],marks1 [ 1 ],marks2 [ 1 ]} ; 



编辑

如果阵列中的数组如下:

  var  marksArray =  new  [] {m​​arks,marks1,marks2} ; 



然后你可以使用LINQ来创建结果数组:

  var  result2 = marksArray.Select(x = >  x [ 1 ])。ToArray(); 



使用for循环工作太多了。


can you help me to read the second value in array
i mean :
int [] marks = new int[5] { 99, 98, 92, 97, 95};
int [] marks1 = new int[5] { 13, 19, 5, 4, 2};
int [] marks2 = new int[5] { 9, 1, 17, 288, 14};
I want read just the second value from those arrays : 98 , 19 , 1
and put it in a new array how i can do it ? in for loop i mean
thank you .

解决方案

In my opinion, getting the values from the array would be a very much good idea, like this,

int[] arr = { marks[1], marks1[1], marks2[1] };



Enough. It would have an array of 3 elements having the value of second element in those three arrays.

Otherwise, if you want to get the values using the loop. You would write the loop and get the value using the index variable. But, remember, you only want one value so do not waste CPU cycles on extra loops. Write it this way,

int a = 0;
for (int i = 1; i < 2; i++ ) { a = marks[i]; }



The above loop would run only once, because in the first time the condition would be true, then it would be false so after getting the value at index 1 (element 2) it would break. Write it this way,

int[] arr = { 0, 0, 0 }; // arbitrary data (not required)

for (int i = 1; i < 2; i++) { arr[0] = marks[1]; }
for (int i = 1; i < 2; i++) { arr[1] = marks1[1]; }
for (int i = 1; i < 2; i++) { arr[2] = marks2[1]; }

// Possible value would be
{ 98, 19, 1 }



There you go.


Why would you need a for loop?

var result = new [] {marks[1],marks1[1],marks2[1]};


Edit
If you had the arrays in an array like this:

var marksArray = new [] {marks, marks1, marks2};


Then you could use LINQ to create the result array:

var result2 = marksArray.Select(x => x[1]).ToArray();


Using for-loop is just too much work.


这篇关于在for循环中读取数组中的第二个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆