在具体情况下如何将此数组转换为列表?

时间:2015-01-14 19:17:19

标签: java arrays loops for-loop arraylist

我已遍历数组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;

            }

        }

    }

}

3 个答案:

答案 0 :(得分:1)

当您打印像ArrayList之类的对象时,您正在表达的打印行为,Java称为Class .toString()方法。 ArrayList.toString()继承自AbstractCollection.toString(),可以找到哪些文档here

我建议更改此行为的方法是创建一个类似于以下内容的精确打印功能:

public void printList(ArrayList<String> list)
{
     for(String str : list)
     {
          System.out.print(str+" ");
     }
     System.out.println();
}

然后调用此函数代替print语句:

printList(crIss);

请注意,此打印件会在一行上打印List的每个元素,并以空格分隔。

答案 1 :(得分:0)

有几种方法可以做到这一点。我会告诉你三个:

  1. 将数据存储在String中并删除不需要的字符:

    String tmp = crIss.toString();
    tmp = tmp.substring(1, tmp.length() - 1);
    System.out.println(tmp);
    
  2. 与上述方法类似,但使用replace

    String tmp = crIss.toString().replace('[', '\0').replace(']', '\0');
    System.out.println(tmp);
    
  3. 迭代List并将值存储在StringBuilder中:

    StringBuilder sb = new StringBuilder();
    for (String string : crIss) {
        sb.append(string).append(", ");
    }
    sb.remove(sb.length() - 2, sb.length());
    System.out.println(sb.toString());
    

答案 2 :(得分:-1)

只需遍历crIss并打印其中的每个项目,然后打印所需的分隔符。在此示例中,我将使用&#34;,&#34;。

分隔项目
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中的每个项目。