为什么我不能从ArrayList获取值?

时间:2014-12-30 11:52:13

标签: java arraylist

所以,我正在尝试制作一个破解器,而且我似乎无法从ArrayList获取当前数字。我正在使用pin.get(currentChar)来获取它。看看这个:

package me.pta2002.pincrack;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    static Scanner scanner = new Scanner(System.in);

    public static List<Integer> pinNumbers = new ArrayList<Integer>();

    public static void main(String[] args) {
        System.out.println("Insert your first pin number");
        int num1 = scanner.nextInt();
        System.out.println("Insert your second pin number");
        int num2 = scanner.nextInt();
        System.out.println("Insert your third pin number");
        int num3 = scanner.nextInt();
        System.out.println("Insert your fourth pin number");
        int num4 = scanner.nextInt();
        pinNumbers.add(num1);
        pinNumbers.add(num2);
        pinNumbers.add(num3);
        pinNumbers.add(num4);
        System.out.println("Starting the crack!");

        int currentChar = 0;
        int currentNum = 0;

        String pin = "" + pinNumbers.get(0) + pinNumbers.get(1) + pinNumbers.get(2) + pinNumbers.get(3);
        List<Integer> crackedPin = new ArrayList<Integer>();
        crackedPin.add(0);
        crackedPin.add(0);
        crackedPin.add(0);
        crackedPin.add(0);
        String pinCracked = ""+crackedPin.get(0) + crackedPin.get(1) + crackedPin.get(2) + crackedPin.get(3);

        while(pinCracked!=pin){
            if(currentChar<5){
                currentNum++;
                if(currentNum==pinNumbers.get(currentChar)){
                    crackedPin.remove(currentChar);
                    crackedPin.add(currentChar,currentNum);
                    currentNum=0;
                    currentChar++;
                }
            }
        }
        System.out.println(crackedPin);
    }



}

现在,我不知道问题是什么,我想得到一些帮助。这是错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at me.pta2002.pincrack.Main.main(Main.java:42)

请帮我这样做。 (我知道我可以改进引脚输入,我稍后会这样做)

4 个答案:

答案 0 :(得分:2)

currentChar为4时,您将超出界限:

if(currentNum==pinNumbers.get(currentChar))

pinNumbers有4个元素,由于数组是基于Java的 0 ,因此索引的范围为[0,N-1]。

在您的循环中,将currentChar < 5更改为currentChar < 4

注意:您要将String==进行比较,您应该使用equals而不是==来比较参考,而不是存储在该参考中的值。< / p>

答案 1 :(得分:2)

while(pinCracked!=pin){
        if(currentChar<5){
            currentNum++;
            if(currentNum==pinNumbers.get(currentChar)){
                crackedPin.remove(currentChar);
                crackedPin.add(currentChar,currentNum);
                currentNum=0;
                currentChar++;
            }
        }
    }

你必须在这里改变if。你需要if(currentChar<4) 编辑: 在最后一次运行中,你有4上的currentchar,这是outofbounds

答案 2 :(得分:1)

pinNumbers.get(currentChar)时{p> currentChar == 4超出了范围,因为您只在列表中添加了4个元素。

    while(!pinCracked.equals(pin)){ // another fix, though not related to the exc
        if(currentChar<pinNumbers.size()){ // this is a safer check
            currentNum++;
            if(currentNum==pinNumbers.get(currentChar)){
                crackedPin.remove(currentChar);
                crackedPin.add(currentChar,currentNum);
                currentNum=0;
                currentChar++;
            }
        }
    }

答案 3 :(得分:1)

这是您的代码,完全按照您的意愿运行。阅读更改/添加的注释

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    static Scanner scanner = new Scanner(System.in);

    public static List<Integer> pinNumbers = new ArrayList<Integer>();

    public static void main(String[] args) {
        System.out.println("Insert your first pin number");
        int num1 = scanner.nextInt();
        System.out.println("Insert your second pin number");
        int num2 = scanner.nextInt();
        System.out.println("Insert your third pin number");
        int num3 = scanner.nextInt();
        System.out.println("Insert your fourth pin number");
        int num4 = scanner.nextInt();
        pinNumbers.add(num1);
        pinNumbers.add(num2);
        pinNumbers.add(num3);
        pinNumbers.add(num4);
        System.out.println("Starting the crack!");

        int currentChar = 0;
        int currentNum = 0;

        String pin = "" + pinNumbers.get(0) + pinNumbers.get(1) + pinNumbers.get(2) + pinNumbers.get(3);
        List<Integer> crackedPin = new ArrayList<Integer>();
        crackedPin.add(0);
        crackedPin.add(0);
        crackedPin.add(0);
        crackedPin.add(0);
        String pinCracked = ""; //Changed

        while(!pinCracked.equals(pin)){ //Changed Equating reference before, always true
            if(currentChar<4){ //Changed Out of bound
                currentNum++;
                if(currentNum==pinNumbers.get(currentChar)){
                    crackedPin.remove(currentChar);
                    crackedPin.add(currentChar,currentNum);
                    pinCracked+=currentNum; //Added, Change the string instead
                    currentNum=0;
                    currentChar++;
                }
            }
        }
        System.out.println(pinCracked); //Changed
    }



}