【问题标题】:Java: iterate through two arrays and using correct valuesJava:遍历两个数组并使用正确的值
【发布时间】:2016-03-22 11:12:53
【问题描述】:

给定 2 个 int 数组,ab,返回一个长度为 2 的新数组,其中包含来自a 的元素,后跟来自b 的元素。数组可以是任意长度,包括 0,但在 2 个数组之间会有 2 个或更多可用元素。

我想知道如何使用a !=null. 这是我使用它的第一个问题,我遇到了一些错误。 所以我的逻辑是遍历列表a,直到null 点,然后进入列表b

public int[] make2(int[] a, int[] b) {
      int[] answer = new int[2];
      for(int x = 0; x <= 1; x++){
          if(a[x] != null)
              answer[x] = a[x];
          else if (b[x != null]){
              answer[x] = b[x];
          }
      }

}

类似的东西。有关如何检查空虚的任何提示?

【问题讨论】:

    标签: java list indexing null


    【解决方案1】:
    else if (b[x != null]){
    

    不是一个有效的语句,这将导致错误,因为(x != null) = true 所以与else if (b[true]){ 相同,没有多大意义。

    此外,int 的空array 位置永远不会是null,而是零0

    改用这个:

    else if (b[x] != 0){
    

    还有一件事:正如标题所说,使用List 代替array 来拥有可变数量的元素:

    public int[] make2(int[] a, int[] b) {
          List<Integer> answers = new ArrayList<Integer>();
          for(int x = 0; x <= 1; x++){    
              if(a[x] != 0) {             
                  answers.add(a[x]);
              } else if (b[x] != 0) {
                  answers.add(b[x]);
              }
          }
    }
    

    注意事项:

    • 使用answers 而不是answer,是具有多个元素的List 的更好名称。
    • 照常使用 ALWAYS { 即使里面的语句只有一行
    • 您的函数无效,因为不包括return answer

    编辑

    对不起标题,我的错误。是数组

    在这种情况下,请记住数组不可调整大小,因此在创建数组时检查最大大小:

    int maxSize = a.length > b.length ? a.length : b.length;
    int[] answer = new int[maxSize];
    

    注意:您可以在循环中使用此变量来检查最大数量...


    编辑2

    空数组位置用 0 表示?例子 make2({}, {1, 2}) → {1, 2} 怎么样,a[0] != 0 会跳过吗?因为索引 0 不存在?

    1. 你不能使用make2({}, {1, 2}),在这种情况下不是一个有效的声明。要模拟此操作:

      public static void main(String[] args) {
           int a[] = new int[2];
           int b[] = new int[2];
           b[0] = 1;
           b[1] = 2;
           make2(a,b);
           // if you want to see the output:
           System.out.println(Arrays.toString(make2(a,b)));
      }
      
    2. 它不会跳过它,它会抛出一个ArrayIndexOutOfBoundsException,在这种情况下,你必须让你的函数失效安全,为此,只需在访问元素之前检查长度以断言它是否存在:

      public static int[] make2(int[] a, int[] b) {
            int[] answer = new int[2];
            for(int x = 0; x <= 1; x++){
                if(a.length >= x && a[x] != 0) { 
                    answer[x] = a[x];
                } else if (b.length >= x && b[x] != 0){
                    answer[x] = b[x];
                }
                // optional
                else {
                    answer[x] = -1; // this tells you there's no valid element!
                }
            }
            return answer;
      }
      

    解决方案:

    http://www.codingbat.com/prob/p143461这是问题的网址:D

    好吧,你错过了这些例子,一切都会更清楚......这段代码通过了所有测试。

    public int[] make2(int[] a, int[] b) {
      int[] answer = new int[2];       // create the array to fill
      int y = 0;                       // create a variable to check SECOND array position
      for(int x = 0; x <= 1; x++){     // make 2 iterations
          if(a.length > x) {           // if ARRAY a has a possible value at POSITION x
              answer[x] = a[x];        // put this value into answer
          } else if (b.length > y){    // if ARRAY a does not have possible value at POSITION x, 
                                       // check if ARRAY b has some possible value at POSITION y 
                                       // (remember y is the variable that keeps position of ARRAY b)
              answer[x] = b[y++];      // put b value at answer
          }
      }
      return answer;                   // return answer
    }
    

    【讨论】:

    • 对不起标题,我的错误。它是数组。
    • 实际使用列表或不存储answers 并不重要,使用数组但检查我的编辑并告诉我它是否适合你;)
    • 空数组位置用0表示?例子 make2({}, {1, 2}) → {1, 2} 怎么样,a[0] != 0 会跳过吗?因为索引 0 不存在?
    • 不,这不是一个空位置,是一个完全没有位置的空数组会抛出ArrayIndexOutOfBoundsException
    • 哦,我明白了,您滥用了长度总是高于索引的想法。所以你在比较长度和索引,所以要找到一个可用的索引。长度必须大于索引,并且您正在使用该想法来检查索引是否可用。
    【解决方案2】:

    您可以检查数组是否为字符串,但您的数组是 int。所以你可以使用下面的例子

    int arr[] = null;
    if (arr != null) {
      System.out.println("array is null");
    }
    
    
    arr = new int[0];
    if (arr.length != 0) {
      System.out.println("array is empty");
    }
    

    【讨论】:

      【解决方案3】:

      在我向您推荐一些替代方案之前,我有几个问题。 1. 你为什么要声明 int[] answer = new int[2];当问题说 2 个或更多元素时。

      你应该使用:

      List<int> stockList = new ArrayList<int>();
      
      if(a[x] != null)
          stockList.add(a[x]);
      else if (b[x] != null){
          stockList.add(b[x]);
      }
      
      int[] stockArr = new int[stockList.size()];
      stockArr = stockList.toArray(stockArr);
      

      【讨论】:

      • 因为问题说返回一个长度为 2 的新数组。
      • @NewtoJava,您的问题是,“但 2 个数组之间将有 2 个或更多元素可用。”因此我建议列出一个列表
      猜你喜欢
      • 2019-06-23
      • 2017-04-08
      • 2023-03-10
      • 2021-10-08
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多