【问题标题】:Removing First Element From Array从数组中删除第一个元素
【发布时间】:2017-02-25 20:37:17
【问题描述】:

现在我有一个字符串数组列表。该数组填充了用户在控制台中输入的数据。我正在尝试删除数组的第一个元素,然后使用其余的输入。输入的其余部分是指没有第一个元素的输入。我试图通过使用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);
            }
        }
    }
}

【问题讨论】:

  • Attempting to remove first element from ArrayList,您是想说您想删除用户输入的第一个字符吗?即,如果输入了1000,那么您要删除1。
  • 正确。因此,如果用户输入 101010,我唯一要删除的内容是 1,删除后将显示为 01010
  • 检查我的答案,如果它解决了您的问题,请告诉我。

标签: java arrays methods


【解决方案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);
        }
    }
}

}

【讨论】:

  • 谢谢!与substring 通话愉快。我完全忘记了那个方法。自从我做java以来​​已经有一段时间了。仍然回到事物的摇摆!谢谢^_^
【解决方案2】:

你的目标很简单,在这种情况下你不需要使用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);
        }
    }
}

【讨论】:

    【解决方案3】:

    “不工作”是什么意思?它会产生错误吗? 使用.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);
    

    【讨论】:

    • 刚刚编辑了我的代码。进行转换时,代码应删除第一个元素,如果输入“1000”,则输出 8(转换为二进制)。但我有binaryInput.remove(0);,它应该删除第一个元素,将数组更改为“000”,它应该输出 0
    【解决方案4】:

    else 块中的这一行

    convertedBinary = Integer.parseInt(userInput, 2);
    

    应该是这样的

    convertedBinary = Integer.parseInt(binaryInputJoined, 2);
    

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

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

    【讨论】:

    • 我试过了 :(。它不起作用,因为parseInt 需要一个字符串。可能是binaryInput.toString();?
    • 你可以连接你在 binaryInput 中的值来创建一个新的字符串。
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2011-06-03
    相关资源
    最近更新 更多