我已经遍历了数组B。检查arrayList A中是否存在公共元素。然后删除那些元素,并打印出A作为数组。

但是我的问题是如何打印A(在我的程序中为System.out.println(crIss.get(m));)作为arrayList(只是一个没有[]括号的元素列表)而不是数组?

这是我正在处理的完整代码:

package issuetracking;

import java.util.*;

public class IssueTrackingObject {

    ArrayList<String> crIss = new ArrayList<String>();

    Scanner input = new Scanner(System.in);
    boolean crIss_bool;
    int numOfSolvedIss;
    private String[] solvedIss;

    //lets user create some issues and add them into an arrayList
    public void createIssue() {
        System.out.println("Enter 5 issues: ");

        for (int i = 0; i < 5; i++) {
            System.out.println("Issue " + (i + 1 + ": "));
            crIss_bool = crIss.add(input.nextLine());
        }

    }
//Let user mark some issues as solved (which are already in the list that the user has just created)

    public void solvedIssue() {
        System.out.println("How many solved issue you have(Must be less than 5): ");
        numOfSolvedIss = input.nextInt();

        solvedIss = new String[numOfSolvedIss];

        for (int k = 0; k < numOfSolvedIss; k++) {
            System.out.print("Enter solved issue(REMEMBER THAT THE SOLVED ISSUE MUST BE FROM ONE OF THEM YOU ALREADY HAVE CREATED)no. " + (k + 1) + ": ");

            solvedIss[k] = input.next();
        }

    }

    public void printUnsolvedIssue() {

for(int m=0; m<solvedIss.length;m++){
      crIss_bool = crIss.remove(solvedIss);

    System.out.println(crIss.get(m));
}


    }


    public void printSolvedIssue() {
        System.out.println("");
        System.out.println("LIST OF SOLVED ISSUE:");

        for (int l = 0; l < solvedIss.length; l++) {
            System.out.printf("%s ", solvedIss[l]);
        }

    }

}


主类:

package issuetracking;

import java.util.*;

public class IssueTracking {

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        IssueTrackingObject its = new IssueTrackingObject();

        System.out.println("                            WELCOME TO ISSUE TRACKING SYSTEM!\n\n");
        System.out.println("Choose from menu: ");
        System.out.println("1. Create new issue\n2. Mark issue(s) as solved\n"
                + "3. View solved issue(s)\n4. View unsolved issue(s)");
        System.out.println("Enter you choise:");
        //String userChoise = input.next();
        //switch-case

        while (true) {
            String userChoise = input.next();

            switch (userChoise) {
                case "1":
                    //System.out.println("Enter 5 issues: ");
                    //call appropriate issue
                    its.createIssue();
                    break;
                case "2":
                    //System.out.println("Mark solved issues (You must enter at least one issue): ");
                    //call appropriate issue
                    its.solvedIssue();
                    break;
                case "3":
                    System.out.println("Solved issue: ");
                    //call appropriate method
                    its.printSolvedIssue();
                    break;

                case "4":
                    System.out.println("Usolved issue: ");
                    //call appropriate issue
                    its.printUnsolvedIssue();
                    break;
                default:
                    System.out.println("Invalid input");
                    break;

            }

        }

    }

}

最佳答案

只需遍历crIss并打印其中的每个项目,然后打印所需的分隔符即可。在此示例中,我将用“,”分隔项目。

public void printUnsolvedIssue() {

   for(int m=0; m<solvedIss.length;m++){
       crIss.remove(solvedIss[i]);
   }

   for(int i = 0; i < crIss.size(); i++) {
       System.out.print(crIss.get(i));
       // only print the delimiter if it isn't the last item in the list
       if(i < crIss.size() - 1) {
           System.out.print(", ");
       }
   }
}


还要注意,我将crIss.remove(solvedIss)部分更改为crIss.remove(solvedIss[i])。这样,它将删除solvedIss中的每个项目。

10-05 18:37