Java 我的程序遇到了一点难题(注意不能使用ArrayList)

Java 我的程序遇到了一点难题(注意不能使用ArrayList),java,arrays,Java,Arrays,我的程序由一个大小为25的字符串数组组成。我试图做的是创建一个add方法,将元素添加到此数组中。当我尝试添加相同的字符串时,我创建一个方法,显示该特定项在字符串数组中列出的次数(我使用哈希映射来执行此操作)。计划没有按计划进行。我的add方法似乎有些不对劲,因为当我添加新元素时,它不是递增到数组中的下一个索引,而是在索引0处重新启动(这是有意义的,因为每次调用add方法I=0)。如果添加了内容,如何修改此方法以转到下一个索引?谢谢,请参阅下面的代码: import java.util.*; p

我的程序由一个大小为25的字符串数组组成。我试图做的是创建一个add方法,将元素添加到此数组中。当我尝试添加相同的字符串时,我创建一个方法,显示该特定项在字符串数组中列出的次数(我使用哈希映射来执行此操作)。计划没有按计划进行。我的add方法似乎有些不对劲,因为当我添加新元素时,它不是递增到数组中的下一个索引,而是在索引0处重新启动(这是有意义的,因为每次调用add方法I=0)。如果添加了内容,如何修改此方法以转到下一个索引?谢谢,请参阅下面的代码:

import java.util.*;

public class Assignment1 {




        public static void main(String[] args){
            new Assignment1 ();
        }

        // This will act as our program switchboard
        public Assignment1 (){
            Scanner input = new Scanner(System.in);
            String[] flowerPack = new String[25];
            int[] flowerCount = new int[25];
            System.out.println("Welcome to my flower pack interface.");
            System.out.println("Please select a number from the options below");
            System.out.println("");

            while(true){
                // Give the user a list of their options
                System.out.println("1: Add an item to the pack.");
                System.out.println("2: Remove an item from the pack.");
                System.out.println("3: Sort the contents of the pack.");
                System.out.println("4: Search for a flower.");
                System.out.println("5: Display the flowers in the pack.");
                System.out.println("0: Exit the flower pack interfact.");

                // Get the user input
                int userChoice = input.nextInt();

                switch(userChoice){
                    case 1: 
                        addFlower(flowerPack);
                        break;
                    case 2: 
                        removeFlower(flowerPack);
                        break;
                    case 3: 
                        sortFlowers(flowerPack);
                        break;
                    case 4: 
                        searchFlowers(flowerPack);
                        break;
                    case 5: 
                        displayFlowers(flowerPack);
                        break;
                    case 0: 
                        System.out.println("Thank you for using the flower pack interface. See you again soon!");
                        System.exit(0);
                }
            }

        }

        private void addFlower(String flowerPack[]) {
            // TODO: Add a flower that is specified by the user
            int i = 0;

            String flowerName;
            Scanner flowerInput = new Scanner(System.in);

            System.out.println("Please enter the name of a flower type to add:");
            flowerName = flowerInput.nextLine();
            flowerPack[i] = flowerName;
            i++;

        }

        private void removeFlower(String flowerPack[]) {
            // TODO: Remove a flower that is specified by the user
            String flowerName;
            Scanner flowerInput = new Scanner(System.in);
            System.out.println("Please enter the name of a flower type to remove");
            flowerName = flowerInput.nextLine();

            for (int i = 0; i < flowerPack.length; i++) {
                if (flowerPack[i].equals(flowerName)) {
                    flowerPack[i] = ""; //Will ask professor if we should set to NULL instead
                }
            }
        }

        private void sortFlowers(String flowerPack[]) {
            // TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
            // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings

            Arrays.sort(flowerPack);
        }

        private void searchFlowers(String flowerPack[]) {
            // TODO: Search for a user specified flower
            String flowerType;
            Scanner flowerInput = new Scanner(System.in);
            System.out.println("Please enter the name of a flower to search for:");
            flowerType = flowerInput.nextLine();

            for (int i = 0; i < flowerPack.length; i++) { //done in O(n) time This is a linear search
                if (flowerPack[i].equals(flowerType)) {
                    System.out.println("Found your flower " + flowerPack[i]);
                    break;
                } else {
                    System.out.println("Invalid flower type!");
                    break;
                }
            }
        }

        private void displayFlowers(String flowerPack[]) {
            // TODO: Display only the unique flowers along with a count of any duplicates
            /*
             * For example it should say
             * Roses - 7
             * Daffodils - 3
             * Violets - 5
             */

            Map<String,Integer> theFlowers = new HashMap<String, Integer>();

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

                if(theFlowers.get(flowerPack[i])==null){
                    theFlowers.put(flowerPack[i],1);
                }else{
                    theFlowers.put(flowerPack[i], theFlowers.get(flowerPack[i])+1);
                }
            }
            System.out.println(theFlowers);

        }



}
import java.util.*;
公共课堂作业1{
公共静态void main(字符串[]args){
新转让1();
}
//这将作为我们的节目总机
公开转让1(){
扫描仪输入=新扫描仪(System.in);
字符串[]flowerPack=新字符串[25];
int[]flowerCount=新int[25];
System.out.println(“欢迎来到我的花包界面”);
System.out.println(“请从下面的选项中选择一个数字”);
System.out.println(“”);
while(true){
//为用户提供他们的选项列表
System.out.println(“1:将项目添加到包中”);
System.out.println(“2:从包中移除一个项目”);
System.out.println(“3:对包中的内容进行排序”);
System.out.println(“4:搜索一朵花”);
System.out.println(“5:显示包装中的花”);
System.out.println(“0:退出花卉包装接口”);
//获取用户输入
int userChoice=input.nextInt();
开关(用户选择){
案例1:
addFlower(花卉包装);
打破
案例2:
摘花(花卉包装);
打破
案例3:
酸模花;
打破
案例4:
探索花(花卉包装);
打破
案例5:
展示花卉(花卉包装);
打破
案例0:
System.out.println(“感谢您使用flower pack界面,再见!”);
系统出口(0);
}
}
}
私人花卉(串花套[]){
//TODO:添加用户指定的花
int i=0;
字符串名称;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入要添加的花类型的名称:”);
flowerName=flowerInput.nextLine();
花包[i]=花名;
i++;
}
私人空间移除花(串花包[]){
//TODO:删除用户指定的花
字符串名称;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入要删除的花类型的名称”);
flowerName=flowerInput.nextLine();
对于(int i=0;i对于(int i=0;i您应该了解数组中有多少个项目。使用该数字可以查看要使用的索引。

您应该了解数组中有多少个项目。使用该数字可以查看要使用的索引。

您想添加最多25个花然后停止,还是想在添加第26个花时覆盖第一个字符串

如果您只想添加最多25个花,最简单的方法是在数组中的第一个位置添加一个空字符串。 差不多

flowerName = flowerInput.nextLine();
for(int i=0; i<flowerPack.length; i++){
    if(flowerPack[i] == null || flowerPack[i].equalsIgnoreCase("")){
        flowerPack[i] = flowerName;
        break;
    }
}
flowerName=flowerInput.nextLine();

对于(int i=0;i是否希望在最多添加25朵花后停止,还是希望在添加第26朵花时覆盖第一个字符串

如果您只想添加最多25朵花