【问题标题】:Processing mapping always out of bounds处理映射总是越界
【发布时间】:2014-04-24 23:41:01
【问题描述】:

我正在尝试制作一个正在处理的图表,但不断收到“ArrayIndexOutOfBounds”错误。我很难理解 map() 语法的概念,有人可以帮我弄清楚吗?我不确定 map(x,y,z,a,b) 中的每个数字应该代表什么;

String[] name = {
  "1st:",
  "5th:",
  "10th:",
  "15th:",
  "20th:",
  "25th:",
  "30th:"
};

int[] temperature = {
  81,
  82,
  84,
  85,
  87,
  88,
  90
};


void setup(){
  size(200,200);
}

void draw(){
  int x=0;
  for(int i=0;i<10;i++){

    if(mouseX>x && mouseX<=x+40){
      fill(255,40,40);
    }else{
      fill(50);
    }

    float h = map(temperature[i], 0, 100, 0, 200);

    rect(x+4,height-h,32,h);

    x+=40;
  }

}

【问题讨论】:

  • 你能发布你得到的异常的堆栈跟踪吗?
  • map()方法属于哪个类?
  • 您没有向我们展示您的 map() 方法或它的文档,因此我们无法猜测,但您的 ArrayIndexOutOfBoundsException 的原因非常清楚,请参阅我的回答。

标签: java maps processing


【解决方案1】:

您的数组有 7 个元素,但您迭代了 10 次。

for(int i=0;i<10;i++){
  ...
  float h = map(temperature[i], 0, 100, 0, 200);

}

要么用额外的 3 个元素填充数组,要么更好:你应该使用 i &lt; temperature.length() 作为 for 循环中的条件,而不是数字 10。

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多