简体   繁体   English

根据用户输入从Arraylist中删除文本对象

[英]Remove text object from an Arraylist based on userinput

I am creating a program that allows the user to add issues and remove them (based on a menu). 我正在创建一个程序,该程序允许用户添加问题并将其删除(基于菜单)。

My first question: How can I display my "issue" arraylist in another method? 我的第一个问题:如何以另一种方法显示“问题”数组列表? (In this case, "your issues are: +issue array) (在这种情况下,“您的问题是:+问题数组)

My second question: How can I remove an issue from the issue array list WITH user input and store it into a "solved" array list in my second method? 我的第二个问题:如何使用用户输入从问题数组列表中删除问题,并使用第二种方法将其存储到“已解决的”数组列表中?

Code snippet : 程式码片段

public class IssueTrackingObject {

int length; 
int solved; 
Scanner input = new Scanner(System.in); 

public void createIssue() {
    System.out.println("Enter the number of issues you will  enter: ");
    length = input.nextInt(); 
    String[] issues = new String[length]; //issues stored in this array
 for(int counter = 0; counter < length; counter++){
     System.out.println(counter+1 + (") Enter the first issue: ")); 
    issues[counter] = input.next(); 
 }
    System.out.println("Your issues are: ");  
    for(int counter = 0; counter < length; counter++){
 System.out.println(issues[counter]);
    }     
}

void solvedIssue() {
    System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
    solved = input.nextInt(); 
    String [] issues = new String [solved]; 
    for(int s = 0; s < length; s++){
 System.out.println(issues[s]);

}
}
void printSolvedIssue() {
    System.out.println("You have SOLVED the following issues: "); 
}

void printUnsolvedIssue() {
    System.out.println("Your unsolved Issues are: "); 
}
}

I have read everything I can find online and in my book but I cannot find a solution that fits what I want my code to do. 我已经阅读了可以在网上和书中找到的所有内容,但找不到适合我的代码的解决方案。

Issue #1 第1期
You have to make your issues lists members of the class so that they can be accessible from any method inside the IssueTrackingObject class. 您必须使问题列表成为该类的成员,以便可以从IssueTrackingObject类内的任何方法访问它们。

Issue #2 第2期
You can add a SolvedIssues list to store those issues removed from the total issues list. 您可以添加SolvedIssues列表来存储从总问题列表中删除的那些问题。

You can try implementing you IssueTrackingObject class as follows: 您可以尝试如下实现您的IssueTrackingObject类:

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


    public class IssueTrackingObject {
        Scanner input = new Scanner(System.in);
        // Make your issues lists members of the class so that they
        // can be accessed from all the methods inside the class
        private List<String> _allIssues = new ArrayList<String>();
        // Added a Solved issues list to store the solved issues
        private List<String> _solvedIssues = new ArrayList<String>();

        public void createIssue() {
            System.out.println("Enter the number of issues you will  enter: ");
            int length = input.nextInt();
            for (int counter = 0; counter < length; counter++) {
                System.out.printf("Enter issue number %d\n", counter + 1);
                _allIssues.add(input.next());
            }
            System.out.println("Your issues are: ");
            for (int counter = 0; counter < _allIssues.size(); counter++) {
                System.out.printf("%d. %s\n", counter + 1, _allIssues.get(counter));
            }
        }

        void solvedIssue() {
            if(_allIssues.size() == 0) {
                System.out.println("No issues entered yet. Please enter some issues");
                return;
            }
            System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
            int index = input.nextInt();
            --index; // List starts at zero
            _solvedIssues.add(_allIssues.get(index));
            _allIssues.remove(index);
        }

        void printSolvedIssue() {
            System.out.println("You have SOLVED the following issues: ");
            for(int i = 0; i < _solvedIssues.size(); ++i) {
                System.out.printf("%d. %s\n", i + 1, _solvedIssues.get(i));
            }
        }

        void printUnsolvedIssue() {
            System.out.println("Your unsolved Issues are: ");
            for(int i = 0; i < _allIssues.size(); ++i) {
                System.out.printf("%d. %s\n", i + 1, _allIssues.get(i));
            }
        }

        public static void main(String[] args) {
            int exit = 1;
            Scanner input = new Scanner(System.in);
            IssueTrackingObject itracker = new IssueTrackingObject();
            while(exit != -1) {
                System.out.println("Select an Option or -1 to exit\n1. Create Issue\n2. Solve Issue\n"
                                  + "3. View solved issues\n4. View unsolved issues");
                exit = input.nextInt();
                switch (exit) {
                case(0) :
                    System.out.println("Number of issues has to be greater than zero");
                break;
                case(1) :
                    itracker.createIssue();
                break;
                case(2) :
                    itracker.solvedIssue();
                break;
                case(3) :
                    itracker.printSolvedIssue();
                break;
                case(4) :
                    itracker.printUnsolvedIssue();
                break;
                }

            }
            input.close();
            System.out.println("Program finished");
        }
    }

Feel free to make modifications if I didn't quite get what you were trying to do. 如果我不太了解您要执行的操作,请随时进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM