Приведенный ниже код - это класс 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, методам наибольшего и наименьшего размера. Может ли кто-нибудь помочь мне в этом
Спасибо