猿问

我想运行我的代码,但终端告诉索引越界。可能是因为 ArrayList 方法会像这样

我正在尝试运行我的代码。但终端告诉:


Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

    at java.util.ArrayList.rangeCheck(Unknown Source)

    at java.util.ArrayList.get(Unknown Source)

    at CF455A.main(CF455A.java:25)

第 25 个字符串是: game.remove(game.get(max+1)); 我的代码:


import java.util.*;

import java.lang.*;

import java.io.*;


public class CF455A {

 public static void main(String args[]) throws java.lang.Exception {

     Scanner in = new Scanner (System.in);

     int n = in.nextInt();

     int max = 0;

     ArrayList<Integer> game = new ArrayList<Integer>();


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

        int t = in.nextInt();

        game.add(i, t);

    }


    int counter = 0;


    while(game.size()>0){

        for(int j = 0; j < game.size(); j++){

            if(max <= (int)game.get(j)){max=j;}

            for(int i = 0; i < game.size(); i++){

                counter = counter + (int)game.get(max);

                game.remove(game.get(max));

                game.remove(game.get(max+1));

                game.remove(game.get(max-1));

            }

        }


    }

    System.out.print(counter);

    in.close();

}

我认为这是因为 ArrayList 或像game.get();or 之类的方法game.remove();。我只是想更改 ArrayList 的元素之一的值


白板的微信
浏览 114回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答