【问题标题】:How do I check for an index within a double array?如何检查双精度数组中的索引?
【发布时间】:2014-08-16 14:32:19
【问题描述】:
int[][] mytime = {
        {10, 33},  
        {11, 23},  
        {9, 13},  
        {16, 53},
};

for (int h=1; h < mytime[0]; h++)  // checking for the first index (ie: 10, 11, 9, 16)
for (int m=1; h < mytime[1]; h++)  // checking for the second index (ie: 33, 23, 14, 53)

如何执行 for 循环直到其中一项索引检查?

【问题讨论】:

    标签: java android arrays if-statement for-loop


    【解决方案1】:

    我不确定您所说的 one of the index checks 究竟是什么意思,但以下是获取第一列和第二列数字的方法:

    for (int i = 0; i < mytime.length; i++) {
        int first = mytime[i][0];
        int second = mytime[i][1];
    }
    

    您必须迭代从0mytime.length-1 的行。

    【讨论】:

      【解决方案2】:

      你会做某事

      int[][] mytime = {
              {10, 33},  
              {11, 23},  
              {9, 13},  
              {16, 53}
      };
      
      int[] firstIndex = new int[mytime.length];
      int[] secondIndex = new int[mytime.length];
      for (int h = 0; h < mytime.length; h++) {
          firstIndex[h] = mytime[h][0];
          secondIndex[h] = mytime[h][1];
      }
      

      【讨论】:

      • 是的,对不起。最近做了很多 PHP,所以我有时会搞混。
      【解决方案3】:

      您的帖子没有显示您尝试了什么,以及您得到的结果/失败。这将有助于人们更好地了解您的问题。

      public class TotoTo {
          public void foo() {
              int[][] mytime = { { 10, 33 }, { 11, 23 }, { 9, 13 }, { 16, 53 }, };
      
              for (int h = 0; h < mytime.length; h++) {
                  System.out.println(mytime[h][0]);
              }
              for (int h = 0; h < mytime.length; h++) {
                  System.out.println(mytime[h][1]);
              }
          }
      
          public static void main(String... params) {
              new TotoTo().foo();
          }
      }
      

      请注意,在 Java 中(与许多其他编程语言一样,数组索引从 0 开始,而不是从 1。

      您还必须小心这种结构:如果您的 mytime 数组的内容在某个时间点发生变化怎么办?你可以得到一个 ArrayOutOfBoundException...

      int[][] mytime = { { 10 }, { 11, 23 }, { 9, 13 }, { 16, 53 }, };
      

      【讨论】:

        【解决方案4】:

        我假设您正在尝试打印您分配的所有值。在这种情况下,代码如下:

        int[][] mytime={{10, 33},  
                {11, 23},  
                {9, 13},  
                {16, 53},
            };
            for(int h=0;h<4;h++)//for the rows i.e 10,11,9,16
            {
                for(int m=0;m<2;m++)//for the columns i.e 33,23,13,53
                {
                System.out.println(mytime[h][m]);
                }
            }
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-17
          • 2021-11-21
          • 1970-01-01
          • 1970-01-01
          • 2017-11-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多