添加到字符串数组

Adding to a string array

处理封装的程序在将用户输入添加到数组时遇到问题。而且这里很可能还有其他问题。一个是当用户输入选项 3 时显示输出。另外,我不知道如何告诉用户除非他们删除一个项目,否则他们不能再添加到包中。在我什至担心删除之前,我只是想弄清楚添加和显示。

import java.util.ArrayList;
import java.util.Scanner;

class Bag {
    private String[] bag = new String[5];

    public String[] bag() {
        return bag;
    }

    public void add(String bag) {
        for (int i = 0; i < bag.length(); i++) {
            //...
        }
        return;
    }

    public void remove(String bag) {
        return;
    }

    void display() {
        System.out.println("The contents are: " + this.bag());
    }

}

这是第二个class:

import java.util.ArrayList;
import java.util.Scanner;


public class testBag {

    public static void main(String[] args) {
        cart obj = new cart();
        int menu;
        int choice;
        choice = 0;

        Scanner input = new Scanner(System.in);
        ArrayList<testcart> cart = new ArrayList<>();


        System.out.println(" 1. Add item ");
        System.out.println(" 2. Remove item ");
        System.out.println(" 3. Display All");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        while (menu != 4) {

            switch (menu) {
                case 1:
                    while (choice != 2) {
                        System.out.println("What do you want to enter: ");
                        String bag = input.next();
                        obj.add(bag);

                        System.out.println("Enter another? 1: Yes, 2: No");
                        choice = input.nextInt();
                    }
                    break;

                case 2:
                    System.out.println("Enter item to Remove: ");
                    friends.remove(input.next());
                    break;

                case 3:
                    for (int i = 0; i < obj.bag().length; i++) {
                        obj.display();
                    }
                    break;
            }
            System.out.println(" 1. Add item ");
            System.out.println(" 2. Remove item ");
            System.out.println(" 3. Display All items ");
            System.out.println(" 4. Exit ");
            menu = input.nextInt();
        }
    }
}

你的 Bag class 必须有一个计数器来计算它已经有多少个包,并在相应的位置存储一个新包并递增它。

  • 要显示包,您不能 System.out.println 直接数组
  • 要移除,您需要遍历所有袋子并将它们从找到要移除的袋子的位置向左移动。

在您的 Bag class 中实现所有这些:

public class Bag {
    private String[] bag = new String[5];
    private int count = 0; //the new count here

    public void add(String bagToStore) {
        if (count < bag.length){
            bag[count] = bagToStore; //store the new bag in the current position
            count++; //then increment it
        }
    }

    //the remove has more logic because it has to shift the bags if it removes one, 
    //not to leave wholes in the array
    public void remove(String bagToRemove) {
        boolean found = false;
        for (int i=0;i < count; ++i){
            if (bag[i].equals(bagToRemove)){ //to compare Strings you must use equals
                found = true;
            }

            if (found && count < bag.length){
                bag[i] = bag[i+1];
            }
        }

        if (found) count--; 
    }

    void display() {
        for (int i = 0; i < count; ++i) //the display has to be done with a for 
            System.out.println("The contents are: " + bag[i]); 
    }
}

您的主要 class 现在也必须进行调整:

public static void main(String[] args) {
    Bag obj = new Bag();
    int menu, choice = 0;

    Scanner input = new Scanner(System.in);
    do {
        //only print the menu once, you can use a do while for that
        System.out.println(" 1. Add item ");
        System.out.println(" 2. Remove item ");
        System.out.println(" 3. Display All");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        switch (menu) {
        case 1:
            while (choice != 2) {
                System.out.println("What do you want to enter: ");
                obj.add(input.next()); //you call add with input.next as well if you want

                System.out.println("Enter another? 1: Yes, 2: No");
                choice = input.nextInt();
            }
            break;
        case 2:
            System.out.println("What do you want to remove: ");
            obj.remove(input.next()); //just call the remove method on Bag
            break;
        case 3: obj.display(); break; //call the display you already implemented!
        }

    } while (menu != 4);
}

您的 Bag 实现中存在一些问题 class

  1. 您已命名 String 数组来存储您的元素作为 bad 并且 add 方法的参数也作为 bag,因此在 add 函数中 bag 被视为 String 而不是 String 数组。

  2. 在将元素添加到包中之前,您没有检查包的当前大小,您可以创建一个名为 bag 的变量并在添加元素时递增它,并在删除元素时递减它。

  3. 在显示方法中,您直接打印字符串数组而不是数组元素。

我已通过更正这些错误更新了您的 class

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

class Bag
{
    private String bag[] = new String[5];
    int size = 0;

    Scanner scanner = new Scanner(System.in);

    String[] array = new String[2];

    public String[] bag(){
        return bag;
    }

    public void add(String item)
    {
        if( size < bag.length)
        {
            bag[size] = item;
            size++;

        }
        else
        {
            System.out.println("Bag is full remove item before new insertion");
        }
        return;
    }

    public void remove(String item)
    {
    }

    void display(){
        System.out.println("The contents are: " + Arrays.toString(bag));

    }

}