填充数组后忽略其他参数java

编码:


//takes in the parameters by int and string and builds an array

public void setParameter(int newParameterNum, String newParameterName) {


    this.parameterNum = newParameterNum;


    this.parameterName = newParameterName;


    this.parameterList = new String[numParameters];


    this.parameterList[parameterNum] = parameterName;


    for (int i = 0; i < numParameters; i++) {


         this.countParameter += i;

    }



}

主要代码(不可编辑):


//Add the needed parameters, Report should make sure I am not trying to break it

    report1.setParameter(0, "01/01/1970");

    report1.setParameter(1, "01/01/2018");

    report1.setParameter(2, "pjdt");

错误:


run: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2   at

week6project.Report.setParameter(Report.java:134) at week6project.Week6Project.main(Week6Project.java:34) C:\Users\nicho\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml: 53:Java 返回:1 BUILD FAILED(总时间:0 秒)


那么,如何忽略我不想添加到数组中的参数?如果请求的参数数量少于传递的参数。


即它要求2 parameters但3在这种情况下通过。


The numParameters is updated through this method:

public void setNumParameters(int newNumParameters) {


    this.numParameters = newNumParameters;


}

设置的主要方法:


 //Set the number of parameters for each report

    report1.setNumParameters(2);

    report2.setNumParameters(4);


米琪卡哇伊
浏览 150回答 1
1回答

BIG阳

你得到的错误java.lang.ArrayIndexOutOfBoundsException,当您尝试访问数组元素index>= array.length。在下面的代码行中,确保&nbsp;0 <= parameterNum < numParametersthis.parameterList[parameterNum]&nbsp;=&nbsp;parameterName;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java