如何消除数组列表中的空白项?

4
以下是具有主方法的Hello类的代码:
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");
    }
}

第二个类 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);

    }
}

我希望将用户输入的字符添加到数组中(大小由用户给出)。如果大小不足,则要将其加倍,并将所有先前的数组元素复制到新数组中,并将旧数组分配给新数组(参考)。
组成的数组在加倍数组长度时会产生空白。我该如何去除这些空白?
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

空格影响了 Unicode、largest 和 smallest 方法。请问有人能够帮我解决这个问题吗?
谢谢。

为什么不使用ArrayList而是手动管理数组大小呢? - Basil Bourque
FYI,“char”类型现在已经过时,无法表示Unicode定义的14万个字符的一半。请改用Unicode代码点整数。 - Basil Bourque
1个回答

0
空格字符' '具有独特的Unicode值,这会影响对其进行的操作。为了解决这个问题,我建议您使用Unicode值为0的填充字符。例如,在填充charArray时,可以使用空字符进行填充,如下所示:

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

这会影响最小字符的计算,因为这个填充字符具有最低的Unicode值。为了解决这个问题,请确保最小字符的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;
    }

不应该是charArray[i] != Character.MIN_VALUE吗?0在那里是无效的对吧? - Pri ya
实际上,由于类型更改,整数值将很好地工作。在这方面,我对此进行了测试,charArray[i] = 0 似乎可以正常工作。 - Jacob Singer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接