gpt4 book ai didi

java - 我在第 : if ( args[0]. 行的 args[0] 中遇到异常数组索引超出范围等于 ("t") || args[0].equals ("time") ) 当我运行下面的代码时

转载 作者:行者123 更新时间:2023-12-01 22:06:48 25 4
gpt4 key购买 nike

我在下面的代码中遇到了运行时异常ArrayIndexOutOfBoundException,行中:

if ( args[0].equals("t") || args[0].equals("time") )

请帮我解决这个问题。

public static void main(String[] args)   {
System.out.println("Starting Test Cases");
try
{
// Parse the command line args
int policy = 0;
if ( args[0].equals("t") || args[0].equals("time") ) {
policy = ResourceCharacteristics.TIME_SHARED`enter code here`;
}
else if ( args[0].equals("s") || args[0].equals("space") ) {
policy = ResourceCharacteristics.SPACE_SHARED;
}
else {
System.out.println("Error -- Invalid allocation policy....");
return;
}

// determine which test case number to choose
int testNum = Integer.parseInt(args[1]);
if (testNum < MIN || testNum > MAX) {
testNum = MIN;
}

////////////////////////////////////////
// First step: Initialize the GridSim package. It should be called
// before creating any entities. We can't run this example without
// initializing GridSim first. We will get run-time exception
// error.
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // true means tracing GridSim events

// list of files or processing names to be excluded from any
// statistical measures
String[] exclude_from_file = { "" };
String[] exclude_from_processing = { "" };

// the name of a report file to be written. We don't want to write
// anything here.
String report_name = null;

// initialize all revelant variables
double baudRate[] = {1000, 5000}; // bandwidth for even, odd
int peRating[] = {10, 50}; // PE Rating for even, odd
double price[] = {3.0, 5.0}; // resource for even, odd
int gridletLength[] = {1000, 2000, 3000, 4000, 5000};

// Initialize the GridSim package
int totalUser = 2; // total Users for this experiment
GridSim.init(totalUser, calendar, trace_flag, exclude_from_file,
exclude_from_processing, report_name);

//////////////////////////////////////
// Second step: Creates one or more GridResource objects
int totalResource = 3; // total GridResources for this experiment
int totalMachine = 1; // total Machines for each GridResource
int totalPE = 3; // total PEs for each Machine
createResource(totalResource, totalMachine, totalPE, baudRate,
peRating, price, policy);

/////////////////////////////////////
// Third step: Creates grid users
int totalGridlet = 4; // total Gridlets for each User
createUser(totalUser, totalGridlet, gridletLength, baudRate,
testNum);

////////////////////////////////////
// Fourth step: Starts the simulation
GridSim.startGridSimulation();
}
catch (Exception e)
{
System.out.println("Unwanted errors happen");
System.out.println( e.getMessage() );
System.out.println("Usage: java Test [time | space] [1-8]");
}
System.out.println("=============== END OF TEST ====================");
}

执行时会抛出异常,catch block 始终执行。

最佳答案

这意味着您的程序是在没有命令行或 IDE 模拟参数的情况下调用的,因此您的 args 的大小为 0 并引用索引 0将抛出ArrayIndexOutOfBoundsException

如果您从命令行运行此程序,请添加程序参数(例如 java MyMainClass time)。

如果您在 IDE 中运行,则可以将参数添加到运行配置中。

在 Eclipse 中:

  • 右键单击您的项目
  • 选择运行/调试设置
  • 编辑您所需的运行配置(通常只有一个)
  • 选择参数选项卡
  • 添加您的程序参数

注意

一个好的做法是在引用任何数组元素之前检查其大小,例如if (args.length > 0) {...}

关于java - 我在第 : if ( args[0]. 行的 args[0] 中遇到异常数组索引超出范围等于 ("t") || args[0].equals ("time") ) 当我运行下面的代码时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com