在我的特定情况下,如何将此数组转换为列表?
How do I convert this array into list in my specific case?
我已经遍历了一个数组 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]);
}
}
}
主要class:
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
.
中的每个项目
您正在体验的打印行为是因为当您打印一个像 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();
}
然后调用这个函数来代替你的打印语句:
printList(crIss);
请注意,此打印会将列表中的每个元素打印在一行上,并以 space 分隔。
有几种方法可以做到这一点。我给你看三个:
将数据存储在String
中并删除不需要的字符:
String tmp = crIss.toString();
tmp = tmp.substring(1, tmp.length() - 1);
System.out.println(tmp);
与上述方法类似,但使用 replace
:
String tmp = crIss.toString().replace('[', '[=11=]').replace(']', '[=11=]');
System.out.println(tmp);
遍历 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());
我已经遍历了一个数组 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]);
}
}
}
主要class:
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
.
您正在体验的打印行为是因为当您打印一个像 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();
}
然后调用这个函数来代替你的打印语句:
printList(crIss);
请注意,此打印会将列表中的每个元素打印在一行上,并以 space 分隔。
有几种方法可以做到这一点。我给你看三个:
将数据存储在
String
中并删除不需要的字符:String tmp = crIss.toString(); tmp = tmp.substring(1, tmp.length() - 1); System.out.println(tmp);
与上述方法类似,但使用
replace
:String tmp = crIss.toString().replace('[', '[=11=]').replace(']', '[=11=]'); System.out.println(tmp);
遍历
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());