简体   繁体   English

从阵列中删除第一个元素

[英]Removing First Element From Array

right now I have an ArrayList of Strings. 现在我有一个字符串的ArrayList。 The array is filled with data the user enters in the console. 该数组充满了用户在控制台中输入的数据。 I am trying to remove the first element of the array, and then use the rest of the input. 我试图删除数组的第一个元素,然后使用其余的输入。 By the rest of the input I mean the input without the first element. 输入的其余部分是指没有第一个元素的输入。 I am trying to do this by using nameofarray.remove(0); 我试图通过使用nameofarray.remove(0);来做到这一点nameofarray.remove(0); but that does not seem to be working. 但这似乎没有奏效。 Any help on removing the first element of the array and then getting the rest of the array is appreciated, thanks! 任何关于删除数组的第一个元素然后获得阵列其余部分的帮助表示赞赏,谢谢!

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

Your objective is very simple and you need not make use of ArrayList in this case. 您的目标非常简单,在这种情况下您无需使用ArrayList

When msb == '1' , remove the first character and then do the conversion. msb == '1' ,删除第一个字符,然后进行转换。 That's it. 而已。 :-) :-)

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);
        }
    }
}
binaryInput.add(userInput);

BinaryInput is an ArrayList of strings. BinaryInput是字符串的ArrayList。 You're adding the string userInput into binaryInput, so now binaryInput[0] is the string userInput. 您将字符串userInput添加到binaryInput中,所以现在binaryInput [0]是字符串userInput。

binaryInput.remove(0);

This line will remove the first element of binaryInput- which is userInput. 该行将删除binaryInput的第一个元素 - userInput。 I assume you only want to remove the first character of userInput, not the entire string as you're doing in your code right now. 我假设你只想删除userInput的第一个字符,而不是你现在在代码中执行的整个字符串。

I would suggest doing something like this instead: 我建议做这样的事情:

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

} }

What do you mean by "is not working"? 你是什​​么意思“不工作”? Is it producing an error? 它产生错误吗? Using .remove(0) should work, I just made a little test and it removed the first element as expected. 使用.remove(0)应该可以工作,我只做了一点测试,它按预期删除了第一个元素。 The output from the below code is [b, c] . 以下代码的输出是[b, c]

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

This line in your else block 这个行在你的else块中

convertedBinary = Integer.parseInt(userInput, 2);

should be something like 应该是这样的

convertedBinary = Integer.parseInt(binaryInputJoined, 2);

Where you will need to join the values you have in the binaryInput list. 您需要在binaryInput列表中加入值的位置。

It looks like you were either removing from the wrong list, or converting the wrong list. 看起来你要么从错误的列表中删除,要么转换错误的列表。 Alternatively you could avoid lists and use substring. 或者,您可以避免使用列表并使用子字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM