gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:45:21 26 4
gpt4 key购买 nike

下面的代码是具有 main 方法的 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、最大和最小方法。有人可以帮我解决这个问题吗
谢谢

最佳答案

空格字符 ' '有一个唯一的 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;
}

关于java - 如何摆脱数组列表中的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62805607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com