简体   繁体   English

如何声明一个数组列表?

[英]How to declare an array list?

The objective is trying to create a code that gets the difference of the max and min number of the Array List.目标是尝试创建一个代码来获取数组列表的最大和最小数量的差异。

As a beginner I am having trouble understanding why I get that maxRange is not declared in my code.作为初学者,我很难理解为什么我的代码中没有声明 maxRange。

private int firstElement; 
public int maxRange(ArrayList<Integer> arr)
{
  if (maxRange.size() ==0)
  {
      return 0; 
  }
  if (maxRange.size()==1)
  {
      return 1;

  }
  int FirstElement = maxRange.get(1);
  int max = firstElement;
  int min = firstElement;

  for ( int i =0; i < maxRange.size(); i++) 
  {
      int elementValue = maxRange.get(i);
      if(max < elementValue)
      {
          max = elementValue;
      }
      if (elementValue < min)
      {
          min = elementValue;
      }
  }
  return (max -min) + 1;
}
public class Scratchpad
{
   public static void main(String[] args)
   {
       List <Integer> maxRange = new ArrayList <>();

       maxRange.add(3);
       maxRange.add(11);
       maxRange.add(25);
       maxRange.add(48);

       System.out.println(maxRange);
   }
}


If the code listing is all in one file then the problem is that firstElement and maxRange are declared outside of the Scratchpad class.如果代码清单都在一个文件中,那么问题在于firstElementmaxRange是在Scratchpad class 之外声明的。 They should be inside it.他们应该在里面。

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

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