从阵列中删除第一个元素

时间:2016-10-16 23:23:55

标签: java arrays methods

现在我有一个字符串的ArrayList。该数组充满了用户在控制台中输入的数据。我试图删除数组的第一个元素,然后使用其余的输入。输入的其余部分是指没有第一个元素的输入。我试图通过使用nameofarray.remove(0);来做到这一点,但这似乎不起作用。任何有关删除数组的第一个元素然后获取阵列其余部分的帮助表示赞赏,谢谢!

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

class Driver {

    public static void main(String[] args) {
        Scanner k = new Scanner(System.in);
        String userInput;
        char msb;
        int convertedBinary;
        String removedMSB;
        ArrayList<String> binaryInput = new ArrayList<String>();
        System.out.println("Press 1 to use the Binary Converter: Press 2 to use the Decimal Converter:");
        int userChoice = k.nextInt();

        if (userChoice == 1) {
            System.out.println("Welcome to the Binary Converter!");
            System.out.println("Please enter a 10 bit binary number");
            // Adding the input to the ArrayList
            userInput = k.next();
            msb = userInput.charAt(0);
            binaryInput.add(userInput);

            if (msb == '0') {
                convertedBinary = Integer.parseInt(userInput, 2);
                System.out.println("Your number in decimal is " + convertedBinary);
            } else if (msb == '1') {
                // Attempting to remove first element from ArrayList
                binaryInput.remove(0);
            convertedBinary = Integer.parseInt(userInput, 2);
            System.out.println("Your number in decimal is " +      convertedBinary);
            }
        }
    }
}

4 个答案:

答案 0 :(得分:1)

您的目标非常简单,在这种情况下您无需使用ArrayList

msb == '1'时,删除第一个字符,然后进行转换。而已。 : - )

import java.util.Scanner;

class Driver {

    public static void main(String[] args) {
        Scanner k = new Scanner(System.in);
        String userInput;
        char msb;
        int convertedBinary = 0;

        System.out.println("Press 1 to use the Binary Converter: Press 2 to use the Decimal Converter:");
        int userChoice = k.nextInt();

        if (userChoice == 1) {
            System.out.println("Welcome to the Binary Converter!");
            System.out.println("Please enter a 10 bit binary number");
            userInput = k.next();
            msb = userInput.charAt(0);

            if (msb == '0') {
                convertedBinary = Integer.parseInt(userInput, 2);
            } else if (msb == '1') {
                convertedBinary = Integer.parseInt(userInput.substring(1), 2);
            }
            System.out.println("Your number in decimal is " + convertedBinary);
        }
    }
}

答案 1 :(得分:1)

binaryInput.add(userInput);

BinaryInput是字符串的ArrayList。你将字符串userInput添加到binaryInput中,所以现在binaryInput [0]是字符串userInput。

binaryInput.remove(0);

这一行将删除binaryInput的第一个元素 - userInput。我假设你只想删除userInput的第一个字符,而不是你现在在代码中删除的整个字符串。

我建议做这样的事情:

public static void main(String[] args) {
    Scanner k = new Scanner(System.in);
    String userInput;
    char msb;
    int convertedBinary;
    String removedMSB;
    ArrayList<String> binaryInput = new ArrayList<String>();
    System.out.println("Press 1 to use the Binary Converter: Press 2 to use the Decimal Converter:");
    int userChoice = k.nextInt();

    if (userChoice == 1) {
        System.out.println("Welcome to the Binary Converter!");
        System.out.println("Please enter a 10 bit binary number");
        // Adding the input to the ArrayList
        userInput = k.next();
        String withoutFirstChar = userInput .substring(1);
        msb = userInput.charAt(0);

        if (msb == '0') {
            convertedBinary = Integer.parseInt(userInput, 2);
            System.out.println("Your number in decimal is " + convertedBinary);
        } else if (msb == '1') {
            convertedBinary = Integer.parseInt(withoutFirstChar , 2);
            System.out.println("Your number in decimal is " +      convertedBinary);
        }
    }
}

}

答案 2 :(得分:0)

“不工作”是什么意思?它产生错误吗? 使用.remove(0)应该可行,我只做了一点测试,它按预期删除了第一个元素。以下代码的输出为[b, c]

ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.remove(0);
System.out.println(list);

答案 3 :(得分:0)

你的else块中的这一行

convertedBinary = Integer.parseInt(userInput, 2);

应该是

convertedBinary = Integer.parseInt(binaryInputJoined, 2);

您需要加入binaryInput列表中的值。

看起来你要么从错误的列表中删除,要么转换错误的列表。或者,您可以避免使用列表并使用子字符串。