【问题标题】:How do I convert this array into list in my specific case?在我的特定情况下,如何将此数组转换为列表?
【发布时间】:2015-03-13 02:19:21
【问题描述】:

我已经遍历了一个数组 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;

            }

        }

    }

}

【问题讨论】:

  • 你的问题有点混乱。你想从数组中创建一个ArrayList 吗?还是要迭代现有数组并打印出元素?
  • 循环遍历并在新行上打印数组中的每个项目。
  • 是否允许使用其他库,例如 Apache Commons?如果是这样,StringUtils 类中有一个名为 join 的好方法,它将为您完成最困难的部分。
  • printUnsolvedIssue 看起来不对。 m 是一个遍历 solvedIss 的索引。但是,在您从中删除一个元素之后,您使用m 作为crIss 的索引。 crIss 中的索引与solvedIss 中的索引不匹配,因此结果是您将显示错误的元素。您将需要第二个 for 循环。
  • @Riyana,您刚刚完全编辑了代码。一分钟前,它到处都在使用数组——现在它在一些地方使用ArrayList。它应该是哪个?对于一个不断变化的问题,很难写出正确的答案。

标签: java arrays loops for-loop arraylist


【解决方案1】:

只需遍历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 中的每个项目。

【讨论】:

  • 这不是遍历List 元素的好方法。请改用适当的迭代器或增强的 for 循环。
  • 顺便说一句,这不会涵盖 OP 想要/需要的内容:我怎样才能将 A 打印为 arrayList(只是没有 [] 括号的元素列表)而不是数组?
  • 把这个翻译给你(和其他读者):而不是打印"[1, 2, 3]",OP 需要打印"1, 2, 3"。您的代码打印出"1\r\n2\r\n3\r\n
  • forgivenson,size() 方法在这种情况下不起作用。
  • @Riyana 请看我上面的评论。您有一个循环遍历 solvedIss 但使用相同的索引来获取 crIss 元素,这是错误的;这可能就是你认为size() 不起作用的原因。
【解决方案2】:

您正在体验的打印行为是因为当您打印 ArrayList 之类的对象时,Java 会调用该类的 .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 的每个元素,并以空格分隔。

【讨论】:

    【解决方案3】:

    有几种方法可以做到这一点。我给你看三个:

    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());
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2017-03-18
      • 2022-08-11
      • 1970-01-01
      相关资源
      最近更新 更多