我想运行我的代码,但终端告诉索引超出范围。可能,因为ArrayList方法得到ot smth像这样

时间:2023-01-31 20:23:28

I'm trying to run my code. But terminal tells:

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

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)

25th string is: game.remove(game.get(max+1)); My code:

第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();
}

} I think this happends because of ArrayList or methodes like game.get(); or game.remove();. I just wanted to change a value of one of the elements of ArrayList

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

List item

1 个解决方案

#1


1  

According to logic :- if i give input as :- 5 1 2 3 4 5 then first pass :- game.size() = 5; while condition true -> and tries to remove the item at index (1,0,-1).So it fails for (-1) IndexOutOfBounds. Please do the null check as follows :-

根据逻辑: - 如果我输入为: - 5 1 2 3 4 5然后第一次传递: - game.size()= 5; while条件为true - >并尝试删除索引(1,0,-1)处的项目。因此,(-1)IndexOutOfBounds失败。请按如下方式进行空检查: -

import java.util.*;
import java.lang.*;
import java.io.*;

public class TestMemberOuter1 {
    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);
                    if(max < game.size() )
                    {
                        game.remove(game.get(max));
                    }
                    if(max+1 <  game.size() )
                    {
                        game.remove(game.get(max+1));
                    }

                    if(max-1 > 0 )
                    {
                        game.remove(game.get(max-1));
                    }

                }
            }

        }
        System.out.print(counter);
        in.close();
    }
}

#1


1  

According to logic :- if i give input as :- 5 1 2 3 4 5 then first pass :- game.size() = 5; while condition true -> and tries to remove the item at index (1,0,-1).So it fails for (-1) IndexOutOfBounds. Please do the null check as follows :-

根据逻辑: - 如果我输入为: - 5 1 2 3 4 5然后第一次传递: - game.size()= 5; while条件为true - >并尝试删除索引(1,0,-1)处的项目。因此,(-1)IndexOutOfBounds失败。请按如下方式进行空检查: -

import java.util.*;
import java.lang.*;
import java.io.*;

public class TestMemberOuter1 {
    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);
                    if(max < game.size() )
                    {
                        game.remove(game.get(max));
                    }
                    if(max+1 <  game.size() )
                    {
                        game.remove(game.get(max+1));
                    }

                    if(max-1 > 0 )
                    {
                        game.remove(game.get(max-1));
                    }

                }
            }

        }
        System.out.print(counter);
        in.close();
    }
}