如何将主类中的字符串存储到数组中然后进行打印-Java

时间:2018-11-05 03:04:48

标签: java arrays

这不是包含多个类的整个程序,基本上是算法。我遇到的问题是我无法打印出所有选项,并且如果我想先显示标题怎么办,那么将会出现java.lang.ArrayIndexOutOfBoundsException:0错误,如何计算选项的数量:< / p>

public static void main(String[] args)
{

        Menu menu=new Menu("Menu Title");

        menu.display();  

        menu.addOption("Do 1");
        menu.addOption("Do 2");
        menu.addOption("Do 3");
        menu.addOption("Do 4");

        menu.display();

        menu.addOption("Do 5");
        menu.addOption("Do 6");
        menu.addOption("Do 7");
        menu.addOption("Do 8");
        menu.addOption("Do 9");

        menu.display();       
}

public class Menu{
     int countOption;
     String options[];
     String menuTitle;

 public Menu(String menuTitle)
 {
    this.menuTitle = menuTitle;
 }       

 public void addOption(String addOption)      
 {   

    if (addOption != null)
    {
        countOption++;
        options=new String[countOption];
        options[countOption-1]=addOption;

    }   
 }

 public void display()   
 {
       System.out.println(menuTitle);
       int b;
       for (b = 0; b<countOption;b++)    
        System.out.println(options[b]);

  }

}

1 个答案:

答案 0 :(得分:0)

我在这里同意其他人。我认为最好修改 Menu 类,以便它利用ArrayList或List接口(提供)来保存Menu Options,这样它可以根据需要轻松地动态增长,并且您拥有大量的元素控件提供给您的选项。

我采用了您的 Menu 类并对其进行了修改,以便将菜单选项存储在String的列表界面(List<String>)中。我还向该类添加了两个构造函数,其中一个是空的构造函数:

Menu menu = new Menu();

这允许声明菜单,但以后使用任何您喜欢的菜单进行初始化。还有另一个提供标题的构造函数,以及菜单选项的字符串数组或菜单选项的逗号分隔字符串,例如:

String[] mOptions = {"Do 1", "Do 2", "Do 3"};
Menu menu = new Menu("My Title", mOptions);

                  OR

Menu menu = new Menu("My Title", "Do 1", "Do 2", "Do 3");

我还向 Menu 类添加了一些方法,例如 addTitle() ,使 addOption( ) 方法,以接受菜单选项, changeOption() insertOption() removeOption() getOptions() setOptions( )

addTitle()方法允许您供应更改特定菜单实例的标题。

addOption()方法已重载,因此现在可以将菜单选项的字符串数组添加到相关的Menu实例,例如:

String[] newMO = {"New Option 1", "New Option 2"}
Menu menu = new Menu("My New Menu");
menu.addOption(newMO);
menu.display();

changeOption()方法允许您通过提供选项在菜单中的位置的索引值并提供新的选项字符串来更改(重命名或空白)现有菜单选项将替换现有的字符串,例如:

Menu menu = new Menu("\nMy NEW Menu Title :", "1) Do 1", "2) Do 2", "3) Do 3");
menu.display();

menu.changeOption(2, "3) What TO DO");
menu.display();

控制台窗口将显示:

My NEW Menu Title
1) Do 1
2) Do 2
3) Do 3

My NEW Menu Title
1) Do 1
2) Do 2
3) What TO DO

insertOption()方法允许您在相关Menu实例内的特定索引点插入单个菜单选项字符串,例如:

Menu newMenu = new Menu("\nMy NEW Menu Title :", "1) Do 1", "2) Do 2", "3) Do 3");
newMenu.display();
newMenu.insertOption(1, "2) What TO DO"); // Insert new option at index 1
newMenu.changeOption(2, "3) Do 2"); 
newMenu.changeOption(3, "4) Do 3");
newMenu.display();

这是控制台中将显示的内容:

My NEW Menu Title:
1) Do 1
2) Do 2
3) Do 3

My NEW Menu Title:
1) Do 1
2) What TO DO
3) Do 2
4) Do 3

removeOption()方法允许您从位于特定索引的特定Menu实例中删除特定菜单选项,例如:

newMenu.removeOption(1);

         OR

newMenu.removeOption("2) What TO DO");

getOptions()方法允许您为当前Menu实例检索菜单选项的当前列表,例如:

List<String> theMenuOptions = newMenu.getOptions();
System.out.println(String.join(", ", theMenuOptions)); 

setOptions()方法允许您通过传递包含这些新菜单选项的字符串数组来替换相关Menu实例的菜单选项并设置菜单选项的新列表,例如: / p>

String[] newOptions = {"1) Play Sounds", "2) Continue Application", 
                       "3) Exit Application"};
newMenu.setOptions(newOptions); 
newMenu.display();

这是修改后的 Menu 类:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Menu {
     List<String> options = new ArrayList<>();
     String menuTitle;
     String ls = System.lineSeparator();

    // Constructor 1 
    public Menu () {
        // Empty
    } 

    // Constructor 2
    public Menu (String menuTitle) {
        this.menuTitle = menuTitle;
    }

    // Constructor 3
    public Menu (String menuTitle, String... menuOptions) {
        this.menuTitle = menuTitle;
        this.options.addAll(Arrays.asList(menuOptions)); 
    }

    public void addTitle (String title) {
        this.menuTitle = title;
    }

    public void addOption(String menuOption) {   
        this.options.add(menuOption);
    }

    public void addOption(String[] menuOptions) {   
        this.options.addAll(Arrays.asList(menuOptions)); 
    }

    public void insertOption(int atIndex, String optionString) {   
        if (atIndex < 0 || atIndex > this.options.size()-1) {
            throw new IllegalArgumentException(
                    ls + ls+ "Menu.insertOption() Method Error!" + ls +
                    " The supplied Index (" + atIndex + ") is Out Of Bounds!");
        }
        this.options.add(atIndex, optionString);
    }

    public void changeOption(int atIndex, String newOption) {
        this.options.set(atIndex, newOption);
    }

    public void removeOption(int atIndex) {  
        if (atIndex < 0 || atIndex > options.size()-1) {
            throw new IllegalArgumentException(
                    ls + ls+ "Menu.removeOption() Method Error!" + ls +
                    " The supplied Index (" + atIndex + ") is Out Of Bounds!");
        }
        options.remove(atIndex);
    }

    public void removeOption(String optionString) {   
        options.remove(optionString);
    }

    public void display() {
        System.out.println(menuTitle);
        for (int b = 0; b < options.size(); b++) {   
            System.out.println(options.get(b));
        }
    }

    public List<String> getOptions() {
        return options;
    }

    public void setOptions(String[] menuOptions) {
        this.options.clear();
        this.options.addAll(Arrays.asList(menuOptions));
    }
}

以及如何使用此特定类的示例:

String ls = System.lineSeparator();

Menu[] menu = new Menu[4]; // Declare menu as a Menu Array

menu[0] = new Menu();
menu[0].addTitle("This is a Menu which only utilizes a Title." + ls +
        "This Menu instance used Constructor 1 of the " + ls +
        "Menu Class and contains no Menu Options. Of" + ls + 
        "course menu options can be added with the" + ls + 
        "addOption() method");
menu[0].display();  

menu[1] = new Menu(ls + "First TO DO Menu:" + ls +
            "This Menu instance used Constructor 2 of the " + ls +
            "Menu Class and contains 4 Menu Options which" + ls +
            "were added using the addOption() method.");
menu[1].addOption("A) Do 1");
menu[1].addOption("B) Do 2");
menu[1].addOption("C) Do 3");
menu[1].addOption("D) Do 4");
menu[1].display();

String[] mOptions = {"E) Do 5", "F) Do 6", "G) Do 7", "H) Do 8", "I) Do 9"};
menu[2] = new Menu(ls + "Second TO DO Menu:", mOptions);
menu[2].display();       
System.out.println("The above Menu instance used Constructor 3 of" + ls + 
        "the Menu Class and contains 5 Menu Options which" + ls +
        "were added using the mOptions String Array passed" + ls + 
        "to the contructor (see code).");

menu[3] = new Menu(ls + "Third TO DO Menu:", "J) Do 10", "K) Do 11", "L) Do 12");
menu[3].display();
System.out.println("The above Menu instance also used Constructor 3" + ls + 
        "of the Menu Class and contains 3 Menu Options which" + ls +
        "were supplied as comma delimited Strings to the" + ls + 
        "contructor.");

menu[1].addTitle(ls + "First TO DO Menu:" + ls +
                 "Removing 'A) Do 1' from the First menu with" + ls + 
                 "the removeOption() method.");
menu[1].removeOption("A) Do 1");
menu[1].display();
System.out.println("Notice now that 'A) Do 1' is not in the First Menu.");

menu[1].addTitle(ls + "First TO DO Menu - Dynamically Growing:" + ls +
                 "Re-inserting the 'A) Do 1' menu option with the" + ls + 
                 "insertOption() method and adding 3 additional menu " + ls + 
                 "options.");
menu[1].insertOption(0, "A) Do 1"); // Insert option at index 0
menu[1].addOption("Q) Do 5");
menu[1].addOption("R) Do 6");
menu[1].addOption("S) Do 7");
menu[1].display();
System.out.println("Notice now that 'A) Do 1' back in the First Menu" + ls + 
        "and 3 other menu options were dynamically added.");

System.out.println(ls + "Completely changing the First Menu to new menu" + ls +
                        "options using the setOptions() method.");
String[] newOptions = {"1) Play Sounds", "2) Continue Application", 
                       "3) Exit Application"};
menu[1].addTitle(ls + "New First Menu:");
menu[1].setOptions(newOptions);
menu[1].display();

System.out.println(ls + "Adding a new Menu.");
Menu newMenu = new Menu("\nMy NEW Menu Title :", "1) Do 1", "2) Do 2", "3) Do 3");
newMenu.display();

System.out.println(ls + "Inserting a new Menu Item and changing menu items after it.");
newMenu.insertOption(1, "2) What TO DO"); // Insert new option at index 1
newMenu.changeOption(2, "3) Do 2"); 
newMenu.changeOption(3, "4) Do 3");
newMenu.display();