标签: indexoutofboundsexception

如何处理ArrayIndexOutOfBoundsException

我是-1从我的response.我得到这个异常,如何处理这个异常,以便我可以避免任何与我不匹配的整数Array.

Constants.Friends[Integer.parseInt(custom.getFriendsList())]
Run Code Online (Sandbox Code Playgroud)

例如,如果我的数组包含四个项目.

String[] MyList = {"One","Two","Three","Four"};
Run Code Online (Sandbox Code Playgroud)

如果我得到-1或任何大于3的值,我该如何处理它们.

java indexoutofboundsexception

-2
推荐指数
1
解决办法
8548
查看次数

线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:1对输入文件程序

我收到一条消息:线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:1尝试运行以下代码时.该程序从文本文件中获取输入,执行一些工资编号,并输出他们的总工资.

这是文本文件的内容:http: //m.uploadedit.com/b034/139892732049.txt

 package payroll;

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;
 import javax.swing.JOptionPane;

 public class PayRoll
 {
    private String empName;
    private int hours;
    private int hourlyPayRate;

    public PayRoll(String name, int hh, int rr)
    {
        empName = name;
        hours = hh;
        hourlyPayRate = rr;      
    }  

    public String getName()
    {
        return empName;
    }

    public double getPay()
    {
        if(hours <= 40)
            return hours * hourlyPayRate;
        else
            return (40 * hourlyPayRate) + (hours - 40) * 1.5 * hourlyPayRate;
    }

    public static …
Run Code Online (Sandbox Code Playgroud)

java swing indexoutofboundsexception

-2
推荐指数
1
解决办法
323
查看次数

获取“索引超出数组范围”。在 C# 中使用 foreach 循环时

我在尝试使用 foreach 循环打印数组值时在运行时收到“System.IndexOutOfRangeException:'索引超出数组范围。'”错误。我在 Visual Studio 中调试了问题,可以看到 foreach 中的 i 将持续到超出范围的 7。foreach 循环会自动获取数组的所有元素,所以请帮助我理解错误的原因?下面是函数:

    void Sort(int[] A)
    {
        for (int i = 1; i < A.Length; i++)
        {
            int key = A[i];
            int j = i - 1;
            while (j >= 0 && A[j] > key)
            {

                A[j + 1] = A[j];
                j = j - 1;
            

            }
            A[j + 1] = key;
        }

        foreach (int i in A)
            Console.Write(A[i].ToString());

    }

}
Run Code Online (Sandbox Code Playgroud)

}

c# foreach indexoutofboundsexception

-2
推荐指数
1
解决办法
740
查看次数

我不断收到此错误:java.lang.ArrayIndexOutOfBoundsException:12

我的程序基本上采用 ISBN 编号列表并确定它们是否有效。所以错误是:s1[l]=digits[i][l-1]+digits[i][l]; 这行代码使用 for 循环计算 ISBN 编号的部分总和。下面的另一个循环取数组 s1 中所有整数的部分和。请帮忙?错误是:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at ISBN.main(ISBN.java:67)

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

  public class ISBN {
  public static void main(String [] args) throws IOException{
//Reads file line by line
  File ISBNFile = new File ( "isbn_input.txt" );
  Scanner input = new Scanner(ISBNFile);
  String [] preISBN = new String [9999];
  int i = 0;
  int j =0;
  int l =0;
  int m = 0;
  int count =0;
  while (input.hasNextLine()){
    String line …
Run Code Online (Sandbox Code Playgroud)

java eclipse arrays int indexoutofboundsexception

-3
推荐指数
1
解决办法
8287
查看次数

在Java ArrayList中的无效索引处添加元素时不抛出IndexOutOfBoundsException

如果我们在这里读取add函数的ArrayList文档,显然在无效索引处添加元素会抛出IndexOutOfBoundsException.但为什么以下代码不会抛出任何异常?

public static void main(String args[]){
        List cities = new ArrayList();
        cities.add("Atlanta");
        cities.add("Boston");

        try {
            for (int i = 10000000; i < cities.size(); i++){
                try {
                    cities.add(i, "+");
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                System.out.println(cities.size());
            }
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        System.out.println(cities);

    }
Run Code Online (Sandbox Code Playgroud)

输出:

[Atlanta, Boston]

Process finished with …
Run Code Online (Sandbox Code Playgroud)

java arraylist indexoutofboundsexception

-3
推荐指数
1
解决办法
296
查看次数

IndexOutOfBound从最后打印数组

我试图读取从最后一个元素到第一个元素的数组,并在某些情况下更改它们.但我不知道为什么我得到索引超出范围的例外.

public class Test
{
    private Scanner scan = new Scanner ( System.in );

    private int dimension;

    private int[] a;

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

    public Test(int dimension) {
        System.out.println("Add elements");
        setDimension(dimension);
        this.a = new int[this.dimension];
        for(int i = 0; i < this.dimension; i++)
            a[i] = scan.nextInt();
    }

    public void calc() {
        int aux = 0;
        for(int i = a.length-1; i >= 0; i--)
            if(a[i] > a[i-1]) {
                aux …
Run Code Online (Sandbox Code Playgroud)

java indexoutofboundsexception

-3
推荐指数
1
解决办法
55
查看次数

他们为什么不设计从1开始的数组索引?

在许多编程语言中,数组索引从0开始.是否有理由设计它?

据我所知,如果数组的长度等于最后一个索引,那将更方便.我们可以避免大多数ArrayIndexOutOfBounds例外.

当谈到像这样的语言时我能理解C.C是一种古老的语言,开发人员可能没有考虑过这些问题和不适.但是对于像Java这样的现代语言,他们仍然有机会重新定义设计.他们为什么选择保持相同?

它是以某种方式与操作系统的工作有关还是他们真的想继续熟悉的行为或设计结构(尽管新程序员面临很多与此相关的问题)?

java arrays indexoutofboundsexception

-3
推荐指数
1
解决办法
70
查看次数

Java 1.4.2 - ArrayIndexOutOfBounds错误

(我使用的是Java 1.4.2)我希望有一个阵列可以在用户给出的一定时间内翻转"硬币"(硬币可以翻转1000次).然后随机生成1到10之间的整数,将所有数字存储在数组中.我有以下代码,但它一直给我以下错误:

线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:7在CoinFlip.main(CoinFlip.java:42)

这是我写的代码.

import java.io.*;
import java.math.*;

public class CoinFlip
{
    public static void main(String[] args) throws IOException
    {
        // setting up variables
        String timesString;
        int times;
        // setting up input
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));


        do
        {
            // times will display how many times the coin will flip
            // loop if times does not fulfill the requirements
            System.out.println("How many times do you want the coin to flip? (MUST be an integer between 1 to 1000)");
            timesString …
Run Code Online (Sandbox Code Playgroud)

java indexoutofboundsexception

-4
推荐指数
1
解决办法
107
查看次数

IndexOutOfBoundsError错误

我现在很困惑.我有一个ArrayList,我添加一个元素到ArrayList.现在ii尝试list.get(0)并抛出IndexOutOfBounds.......它变得更加奇怪:简而言之它看起来像这样:

ArrayList<SomeCustomClass> list = new ArrayList<SomeCustomClass>();
list.add(new SomeCustomClass());
int index = 0;
for(index = 0;index < list.size();index++){
    if(list.get(index).something < somethingOther){ //Throws no error

        break;
    }
}
SomeCustomClass blarg = list.get(index); //Throws IndexOutOfBounds
Run Code Online (Sandbox Code Playgroud)

...为了确保,我在控制台中测试索引的值,它们都为零,但第二个引发错误.

提前致谢

编辑:Sry.我在问题中犯了错误.现在问题应该是正确的.是的:我检查索引是否有时超过0(它不是),如果"if"为真(它是)

编辑:搞笑:我手动改变指数为0,它是sais:

线程"Thread-2"中的异常java.lang.IndexOutOfBoundsException:索引:0,大小:0

我将0更改为1并且:

线程"Thread-2"中的异常java.lang.IndexOutOfBoundsException:索引:1,大小:1

(我只更改了错误来自的行中的值)Java是否在拖我?>.<:P

java arraylist indexoutofboundsexception

-4
推荐指数
1
解决办法
429
查看次数

访问数组时出错 - 变量'得分'周围的堆栈已损坏

我是C++的新手,并编写了一些代码,其中我收到以下错误:

运行时检查失败#2 - 变量'得分'周围的堆栈已损坏

导致此错误的原因是什么?

这是我的代码:

#include <iostream> // Enables cout and endl
#include <string>
#include <sstream>
#include "stdafx.h"

using namespace std;
int getInput();

int main()
{
    int scores[5];
    int i;
    int j;
    int numberOfScores;

    for (i = 0; i < 6; i++) // Sets all 5 elements of the array to zero
    {
        scores[i] = 0;
    }

    cout << "How many scores do you have to enter?\n" << endl;
    cin >> numberOfScores;


    for (j = 0; j < numberOfScores; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays indexoutofboundsexception

-6
推荐指数
1
解决办法
442
查看次数

为什么我的代码超出范围?

我写了这段代码:

class test {

public static void main(String args[]) {
    int array[] = {1,2,3,4,5,6};
    int i = 0;
    int b = 0;
    int c = 0;


    method.dog(i, b, c, array);
}
}

public class method {

static void dog (int i, int b, int c,  int array[]) {

    if (array[i] <= array[c]) {
           if (c == (int) array.length +1 ) {
                int y = array[i];
                array[i] = array[b];
                array[b] =  y;
                if (b == array.length +1) return;
                else  b++; i …
Run Code Online (Sandbox Code Playgroud)

java quicksort indexoutofboundsexception

-6
推荐指数
1
解决办法
186
查看次数

我的程序给出了索引超出范围的错误

int i=0;    
while(!a.isEmpty()) {         
    if(a.toCharArray()[i]==' ') { 
        System.out.println(a.toCharArray()[i]);         
    }         
    i++;     
}     
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时,它给了我一个错误:索引越界.我该如何解决?

java indexoutofboundsexception

-7
推荐指数
1
解决办法
102
查看次数

如何避免ArrayIndexOutOfBoundsException或IndexOutOfBoundsException?

如果你的问题是java.lang.ArrayIndexOutOfBoundsException在我的代码中得到了一个,我不明白为什么会发生这种情况.它是什么意思,我该如何避免它?

这是关于这个主题的最全面的Canonical信息集合 java.lang.ArrayIndexOutOfBoundsException以及java.lang.IndexOutOfBoundsException.

有很多像这样的问题,所有这些都有模糊的没有代码答案,或者大多数都非常具体,并且局限于手头的问题,并没有解决在所有情况下完全相同的根本原因.


如果您看到属于此一般情况的一个,而不是使用更多重复的专门内容回答它,请将其标记为此副本的副本.

java arrays arraylist indexoutofboundsexception

-9
推荐指数
1
解决办法
4万
查看次数

标签 统计

indexoutofboundsexception ×13

java ×11

arrays ×4

arraylist ×3

c# ×1

c++ ×1

eclipse ×1

foreach ×1

int ×1

quicksort ×1

swing ×1