简体   繁体   English

数组和扩展的困境

[英]Predicament with arrays and expansion

So I know that the java convention is to use ArrayList<> when it comes to expansions and many other applications. 所以我知道Java约定是在扩展和许多其他应用程序中使用ArrayList <>。 The typical array cannot expand. 典型的数组无法扩展。 My java course is elementary so we are still reviewing over arrays right now. 我的Java课程很初级,因此我们现在仍在复习数组。 As much as I want to use an arraylist I cant. 我想使用一个数组列表,我不能。 How do I make it to where I store only elements that satisfy the condition in my counter array? 如何使它满足在计数器数组中仅存储满足条件的元素的位置?

public int[] above100Degrees()
   {
      int[] blazing = new int[temps.length];
      for( int i = 0; i < temps.length; i++ )
      {
         if( temps[i] > 100 )
         {
            blazing[i] = temps[i];
         }
      }
      return blazing;
   }

Output 输出量

The temperature above 100 degrees is:   0   0   0   0   0   0   0   103 108 109

Just count how many elements match your filter first , then create the array, then populate it. 只要看看有多少元素首先匹配您的过滤器,然后创建数组,然后填充它。 It means you'll need to go through the array twice, but there are no really nice alternatives unless you want to end up creating multiple arrays. 这意味着您将需要遍历两次数组,但是没有真正好的替代方法,除非您最终想要创建多个数组。 So something like: 所以像这样:

public int[] above100Degrees() {
    // First work out how many items match your filter
    int count = 0;
    // Are you allowed to use the enhanced for loop? It's not necessary, but it
    // makes things simpler.
    for (int temp : temps) {
        if (temp > 100) {
            count++;
        }
    }

    // Create an array of the right size...
    int[] ret = new int[count];

    // ... and populate it.
    int index = 0;
    for (int temp : temps) {
        if (temp > 100) {
            ret[index++] = temp;
        }
    }
    return ret;
}

I would use a loop to find how many are above 100 before assigning the array. 在分配数组之前,我将使用循环来查找100以上的数量。

public int[] above100Degrees()
{
    int newArrayLength=0;
    for( int i = 0; i < temps.length; i++ )
    {
        if( temps[i] > 100 )
        {
            newArrayLength++;
        }
    }

    int[] blazing = new int[newArrayLength];
    int positionInNewArray = 0;
    for( int i = 0; i < temps.length; i++ )
    {
        if( temps[i] > 100 )
        {
            blazing[positionInNewArray] = temps[i];
            positionInNewArray++;
        }
    }
    return blazing;
}

You could do a ternary operation 您可以进行三元运算

resultString = "The temperature above 100 degrees is: ";
for(int i = 0; i < blazing.length; i++){
    resultString += blazing[i] != 0 ? blazing[i] : "";
}

Note: This would require more memory than JonSkeets answer, but could potentially be more efficient. 注意:这将需要比JonSkeets答案更多的内存,但可能会更有效。 If your expect your array length to get very large, go with JonSkeet's answer. 如果您希望阵列长度很大,请遵循JonSkeet的答案。 In other words, this won't scale well. 换句话说,这将无法很好地扩展。

One way is to count things before setting up the array. 一种方法是在设置阵列之前先进行计数。 Another way is to set up the array first and keep track of the count, then create a new array: 另一种方法是先设置数组并跟踪计数,然后创建一个新数组:

public int[] above100Degrees()
   {
      int[] blazing = new int[temps.length];
      int count = 0;
      for( int i = 0; i < temps.length; i++ )
      {
         if( temps[i] > 100 )
         {
            blazing[count++] = temps[i];
         }
      }

      // At this point, `count` is the number of elements you're going to return;
      // and the first `count` elements of `blazing` hold those elements, while the
      // remaining elements of `blazing` are garbage

      int[] result = new int[count];
      for ( int i = 0; i < count; i++ )
           result[i] = blazing[i];

      return result;
   }

This approach would be better if the condition you're testing for takes a lot of time to calculate (as opposed to temps[i] > 100 , which hardly takes any time). 如果要测试的条件要花费大量时间来计算(与temps[i] > 100 (几乎不需要任何时间)相对),则这种方法会更好。 You could use Arrays.copy to create the result array, but if you can't use ArrayList you probably can't use Arrays.copy either. 您可以使用Arrays.copy来创建结果数组,但是如果您不能使用ArrayList ,则可能也不能使用Arrays.copy

Your code is keeping the cells in blazing array for i <= 100 ; 您的代码将单元格保持在i <= 100 blazing数组中; You need to ignore these and start populating from i = 101 . 您需要忽略这些,并从i = 101开始填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM