全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:4918
推到 Plurk!
推到 Facebook!

java.lang.ArrayIndexOutOfBoundsException: -3 的錯誤

尚未結案
chenglovebun
一般會員


發表:15
回覆:16
積分:6
註冊:2004-10-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-06 03:35:02 IP:69.114.xxx.xxx 未訂閱
import java.io.*;    import java.util.*;    import java.text.DecimalFormat;    import java.io.*;    import java.util.*;    import java.text.DecimalFormat;        public class Process1     {         static double [] input = new double [7];       static int [] inputData = new int [7];       static String line;       static int index=0;       static double simulationTime,nextProcessTime,aProcessTime,ioTime,bound,IoBurst,CpuBurst;              public static void main(String args[]) throws IOException {          BufferedReader  inputFile = new BufferedReader         (new FileReader("params1.txt"));       // Read input          while ((line = inputFile.readLine())  != null) {             input [index]= Double.parseDouble(line);             inputData [index] = (int)input [index]; index++;      }               inputFile.close();              }                              public static void createNewPrpcess()       {           nextProcessTime= (Math.random()*(inputData[index-3]*1.5-inputData[index-3]*0.5) )+inputData[index-3]*0.5;           aProcessTime= (Math.random() *(inputData[index-4]*1.5-inputData[index-4]*0.5) )+inputData[index-4]*0.5;           ioTime= (Math.random() *(inputData[index-1]*1.5-inputData[index-1]*0.5) )+inputData[index-1]*0.5;           bound= (Math.random()*100);                    if (bound<inputData[index-2]) { IoBurst= (Math.random()*20) 20; } else { CpuBurst= (Math.random()*50) 100; } simulationTime = nextProcessTime; System.out.println("created " simulationTime " processes"); } } import java.util.*; public class Event { private static long simuTime,simulaTionTime ; static int i,cpuValue, waitEvent,value; static double endOfsimTime=100000; static PriorityQueue queue = new PriorityQueue(); Revent createRevent = new Revent (); public static void main(String args[]) { long simTime = 0; Process1 p = new Process1 (); p.createNewPrpcess( ); System.out.println(p.simulationTime); createNewEvent( "N",simTime); } public static int cpuEmpty(int value) { cpuValue = value; return cpuValue; } public static void actions(long simulaTionTime){ simuTime=simulaTionTime; while ( simuTime<endOfsimTime) { queue.removeFirst(); simuTime =simuTime; Revent.createRevent(simuTime); } } public static void createNewEvent(Object n, long eventTime ) { queue.insert( n, 0); waitEvent=queue.size( ); } } 出現了Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -3 的錯誤 我想要呼叫p.createNewPrpcess() compile 時出現以上錯誤 該如何修正呢 還有如何從Event class 得到Process1裡的simulationTime值 因為如果我使用p.simulationTime只得到初始值0
chenglovebun
一般會員


發表:15
回覆:16
積分:6
註冊:2004-10-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-06 04:40:21 IP:69.114.xxx.xxx 未訂閱
我解決了compile的錯誤 但是System.out.println("created " p.simulationTime " processes"); 都是0 而不是執行過的值如何修正呢
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-04-06 08:20:09 IP:61.56.xxx.xxx 未訂閱
第一點.請用[code ]....你的程式碼...[/code ],(" ]" 是"]",為了顯示給你看,只好這樣打) 把你的程式碼做排縮 第二點:把你的main加上例外處理:
public static void main(String args[])
{
  try{
    long simTime = 0;
    Process1 p = new Process1 ();
    p.createNewPrpcess( );
    System.out.println(p.simulationTime);        createNewEvent( "N",simTime);
  }catch(Exception exp){
    exp.printStackTrace(); //<----把例外的來龍去脈顯示出來
  }
} 
第三點.把你的陣列元素開大一點: static double [] input = new double [7]; static int [] inputData = new int [7]; 要不要改成 static double [] input = new double [700]; static int [] inputData = new int [700]; 呢 ? 應該夠用了吧
chenglovebun
一般會員


發表:15
回覆:16
積分:6
註冊:2004-10-02

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-06 08:40:20 IP:69.114.xxx.xxx 未訂閱
我試了你的方法但是System.out.println(p.simulationTime);還是0.0
chenglovebun
一般會員


發表:15
回覆:16
積分:6
註冊:2004-10-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-06 09:01:02 IP:69.114.xxx.xxx 未訂閱
是不是因為它是static 變數的源因?
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-04-06 09:40:57 IP:61.64.xxx.xxx 未訂閱
建議你先用IDE去step by step debug 因為大家都很忙.你的code無法花時間幫你看 一般的IDE(JBuilder ,Eclipes)都是可以一步一步去查的
系統時間:2024-04-29 0:03:54
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!