简体   繁体   English

如何摆脱数组列表中的空格?

[英]How to get rid of white spaces in the list of an array?

The code below is the class Hello which has the main method下面的代码是 Hello 类,它有 main 方法

import javax.swing.*;

public class Hello
{
    public static void main(String[] args)
    {
        int size, command;
        char inputChar;
        String inputString;
        //ask a user for an array size
        size = Integer.parseInt(JOptionPane.showInputDialog("Please enter a size for the array"));

        //instantiate a CharacterList Object
        CharacterList list1 = new CharacterList(size);

        //print the menu
        printMenu();

        do
        {
            //ask a user to choose a command
            command = Integer.parseInt(JOptionPane.showInputDialog("Please enter a command number, 1, 2, 3, 4, 5, 6, 7, or 8 (to quit)"));
            System.out.println("Entered command: " + command);
            switch(command)
            {
                case 1: //add a character
                    inputString = JOptionPane.showInputDialog("Please enter a character to add");
                    inputChar = inputString.charAt(0);
                    boolean added;

                    added = list1.addCharacter(inputChar);

                    if(added == true)
                    {
                        System.out.println(inputChar + " was added");
                    }
                    else
                    {
                        System.out.println(inputChar + " was not added");
                    }
                    break;
                case 2: //remove a character
                    inputString = JOptionPane.showInputDialog("Please enter a character to remove");
                    inputChar = inputString.charAt(0);
                    boolean removed;

                    removed = list1.removeCharacter(inputChar);

                    if(removed == true)
                    {
                        System.out.println(inputChar + " was removed");
                    }
                    else
                    {
                        System.out.println(inputChar + " was not removed");
                    }
                    break;
                case 3: //display the array
                    System.out.println(list1);
                    break;
                case 4: //compute and display the largest
                    inputChar = list1.findLargest();

                    if(inputChar == ' ')
                    {
                        System.out.println("\nThe list is empty");
                    }
                    else
                    {
                        System.out.println("\nThe largest character is: " + inputChar);
                    }
                    break;
                case 5: //compute and display the smallest
                    inputChar = list1.findSmallest();

                    if(inputChar == ' ')
                    {
                        System.out.println("\nThe list is empty");
                    }
                    else
                    {
                        System.out.println("\nThe smallest character is: " + inputChar);
                    }
                    break;
                case 6: //compute and display the sum of the unicode
                    System.out.println("\nThe sum of the unicode is: " + list1.computeSumOfUnicode());
                    break;
                case 7:
                    printMenu();
                    break;
                case 8:
                    break;


            }

        } while(command != 8);
    }

    public static void printMenu()
    {
        System.out.print("\nCommand Options\n" +
                "-----------------------------------\n" +
                "1: add a character in the array\n" +
                "2: remove a character from the array\n" +
                "3: display the array\n" +
                "4: compute and display the largest character\n" +
                "5: compute and display the smallest character\n" +
                "6: compute and display the sum of the unicode\n" +
                "7: display the menu again\n" +
                "8: quit this program\n\n");
    }
}

The second class CharacterList第二类CharacterList

import java.util.Arrays;

public class CharacterList {

    private char[] charArray;
    private int count;

    public CharacterList(int arraySize) {

        charArray = new char[arraySize];

        for (int i = 0; i < charArray.length; i++) {
            charArray[i] = ' ';
        }

        count = 0;

    }

    private void doubleArrayCapacity() {

        //create new array of char, which is double length
        char[] newCharArray = new char[this.charArray.length * 2];
        //prescribe values from old array to new one

        for (int i = 0; i < this.charArray.length; i++) {
            newCharArray[i] = this.charArray[i];
        }

        for (int i = this.charArray.length; i < newCharArray.length; i++) {
            newCharArray[i] = ' ';
        }

        //set newCharArray set new value of your field charArray
        this.charArray = newCharArray;
    }


    public int indexOf(char searchingChar) {

        for (int i = 0; i < charArray.length; i++) {

            if (charArray[i] == searchingChar) {
                return i;
            }

        }
        return -1;

    }

    public boolean addCharacter(char characterToAdd) {

        if (indexOf(characterToAdd) == -1) {

            if (count == charArray.length - 1) {

                doubleArrayCapacity();
            }

            for (int i = 0; i < this.charArray.length; i++) {

                if (this.charArray[i] == ' ') {

                    this.charArray[i] = characterToAdd;
                    break;
                }
            }
            count++;
            return true;

        } else
            return false;

    }

    public boolean removeCharacter(char characterToRemove) {

        if (indexOf(characterToRemove) != -1) {

            for (int i = 0; i < charArray.length; i++) {

                if (charArray[i] == characterToRemove) {

                    charArray[i] = charArray[charArray.length - 1];
                    charArray[charArray.length - 1] = ' ';
                }
            }
            count--;
            return true;

        } else
            return false;

    }

    public char findLargest() {

        char largest = charArray[0];

        for (int i = 0; i < charArray.length; i++) {

            if (charArray[i] > largest) {
                largest = charArray[i];
            }
        }
        return largest;
    }

    public char findSmallest() {

        char smallest = charArray[charArray.length - 1];

        for (int i = 0; i < charArray.length; i++) {

            if (charArray[i] < smallest) {
                smallest = charArray[i];
            }
        }
        return smallest;
    }

    public int computeSumOfUnicode() {

        int sum = 0;

        for (int i = 0; i < charArray.length; i++) {
            sum = sum + charArray[i];
        }
        return sum;
    }

    public String toString() {

        return Arrays.toString(charArray);

    }
}

I would like to add the characters which the user types in an array (size given by the user).我想添加用户在数组中键入的字符(用户给定的大小)。 And, if the size is not enough I want to double the size and add copy all the previous array elements in the new array and assign the old array to the new array (referencing).而且,如果大小不够,我想将大小加倍并在新数组中添加复制所有以前的数组元素并将旧数组分配给新数组(引用)。

The array which forms has white spaces which are made while doubling the length of the array.形成的数组具有空格,这些空格是在将数组长度加倍时产生的。 How do I get rid of those white spaces?我如何摆脱那些空白?

Output:
Command Options
-----------------------------------
1: add a character in the array
2: remove a character from the array
3: display the array
4: compute and display the largest character
5: compute and display the smallest character
6: compute and display the sum of the unicode
7: display the menu again
8: quit this program

Entered command: 1
a was added
Entered command: 1
y was added
Entered command: 1
L was added
Entered command: 1
p was added
Entered command: 1
a was not added
Entered command: 1
K was added
Entered command: 1
Y was added
Entered command: 1
S was added
Entered command: 3
[a, y, L, p, K, Y, S,  ,  ,  ,  ,  ]
Entered command: 4

The largest character is: y
Entered command: 5

The list is empty
Entered command: 6

The sum of the unicode is: 813
Entered command: 8

The spaces are disturbing the Unicode, largest, and smallest methods.空格干扰了 Unicode、最大和最小方法。 Could someone help me regarding this有人可以帮我解决这个问题吗

Thank you谢谢

The space character ' ' has a unique unicode value which will mess with the operations done on that.空格字符' '有一个唯一的 unicode 值,它会干扰对其进行的操作。 To get around this, I would recommend making your filler characters that with a unicode value of 0. For example, when your are populating charArray , populating with empty characters could be done with:为了解决这个问题,我建议您使用 unicode 值为 0 的填充字符。例如,当您填充charArray ,可以使用以下方法填充空字符:

charArray[i] = '\u0000'; // Note that \u0000 can be replaced by the integer literal 0

This would mess with the calculation for the smallest character, however, because this filler character has the lowest unicode value.然而,这会干扰最小字符的计算,因为这个填充字符具有最低的 unicode 值。 To get around that, make sure that the unicode value of the smallest character is not 0:要解决这个问题,请确保最小字符的 unicode 值不为 0:

public char findSmallest() {

        char smallest = charArray[charArray.length - 1];

        for (int i = 0; i < charArray.length; i++) {

            if (charArray[i] < smallest && charArray[i] != 0) {
                smallest = charArray[i];
            }
        }
        return smallest;
    }

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

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