Java |.clear()函数的问题
好的,这是我第二次在这里发帖,所以请原谅我提供了太多的信息和我的代码糟糕的状态。这是我正在玩的一个更大的程序的摘录,但我遇到了一个小问题。.clear函数在奇数时间出现错误,我无法查明原因。此功能用于根据输入显示相关的个性类型。输入es会显示es个性类型。其设计用于接收长度为0-4的输入Java |.clear()函数的问题,java,Java,好的,这是我第二次在这里发帖,所以请原谅我提供了太多的信息和我的代码糟糕的状态。这是我正在玩的一个更大的程序的摘录,但我遇到了一个小问题。.clear函数在奇数时间出现错误,我无法查明原因。此功能用于根据输入显示相关的个性类型。输入es会显示es个性类型。其设计用于接收长度为0-4的输入 package MBTI_Experiment; import java.util.Arrays; import java.util.Scanner; import java.util.ArrayList;
package MBTI_Experiment;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class MBTI_Root {
protected int counter = 0;
protected List<String> selected_f = new ArrayList<>();
protected List<String> selected_t = new ArrayList<>();
protected String[] selected_t_1 = new String[16];
protected String[] selected_t_2 = new String[8];
protected String[] selected_t_3 = new String[4];
protected String[] selected_t_4 = new String[2];
protected double[] percentages = new double[] {0.0, 0.0};
protected char[] functions = new char[] {'*', '*', '*', '*'};
protected String[] types_array = new String[] {"intj", "intp", "entj", "entp", "infj", "infp", "enfj", "enfp",
"istj", "isfj", "estj", "esfj", "istp", "isfp", "estp", "esfp"};
protected double[] m_percentages = new double[] {3.3, 4.8, 2.7, 4.0, 1.2, 4.1, 2.7, 6.4,
16.4, 8.1, 11.2, 7.5, 8.5, 7.6, 5.6, 6.9};
protected double[] f_percentages = new double[] {0.9, 1.7, 0.9, 2.4, 1.6, 4.6, 3.3, 9.7,
6.9, 19.4, 6.3, 16.9, 2.3, 9.9, 3.0, 10.1};
public MBTI_Root() {
boolean valid = true; // Resets boolean at the start of function
while (valid) {
reset_Percentages();
System.out.println("\nBelow is a list of possible functions.");
System.out.println("\n 1. Run Gender Distribution \n 2. Run Gender Distribution by Letter " +
"\n 3. Run Gender Distribution by Function ");
System.out.println(" 4. Retrieve Type Information \n 0. Terminate Program ");
System.out.println("\nPlease Enter the Number of the Desired Function.");
Scanner input_1 = new Scanner(System.in);
int option = input_1.nextInt();
// Cut, but continues...
else if (option == 2) {
boolean cont = true;
while (cont) {
reset_Selected_number(selected_t_1);
reset_Selected_number(selected_t_2);
reset_Selected_number(selected_t_3);
reset_Selected_number(selected_t_4);
reset_Selected_t();
reset_Percentages();
reset_Selected_f();
reset_Functions();
System.out.println("\nEnter the letter for desired functions. ");
Scanner input_3 = new Scanner(System.in);
String function = input_3.nextLine().toLowerCase();
MBTI_Function_2(function);
System.out.println("\nSelected function(s): " + selected_f.toString().substring(1,
selected_f.toString().length() - 1) + ".");
boolean not_empty = true;
for (String occupant : selected_t) {
if (occupant.isEmpty()) {
not_empty = false;
break;
}
}
if (not_empty) {
System.out.println("Types that include these functions: " + selected_t.toString().substring
(1, selected_t.toString().length() - 1).toUpperCase());
} else {
System.out.println("There are no types that include this combination of functions.");
}
cont = return_to_root("Enter more inputs?");
}
}
具体来说,我在使用.clear方法的reset_Selected_t时遇到问题。每当输入少于4个字符时,我就会收到一个错误。
这是一个很长很圆的方法来解决这个问题,但是为什么我在这个清晰的函数上遇到了麻烦呢?它的近亲reset_Selected_f工作正常。您的问题是Selected_t=Arrays.asListselected_t_1 Arrays.asList不会基于数组的内容创建新列表;它将现有数组包装在列表中。因此,对列表的更改将反映在数组中 在这种情况下,方法.clear会产生问题,因为它会从列表中删除所有元素。这对于内部数组是不可能的;它总是固定大小的。因此,它抛出一个异常
如果您试图对Array.asList创建的列表使用add或remove,也会发生同样的情况。将Array.asList替换为
selected_t = Arrays.stream(selected_t_1).collect(Collectors.toList());
或
您得到的错误是什么,即堆栈跟踪?您写道:.clear函数出现错误,请回答您的问题并提供您得到的错误的详细信息。是的,我第二次发布的内容越少,在这里发布的内容越多,堆栈跟踪、错误消息的技术细节就越清晰、具体。
protected void reset_Percentages() {
percentages[0] = 0.0;
percentages[1] = 0.0;
}
protected void reset_Selected_f() {
selected_f.clear();
}
protected void reset_Functions() {
Arrays.fill(functions, '*');
}
protected void reset_Counter() {
counter = 0;
}
protected void reset_Selected_t() {
selected_t.clear();
}
protected void reset_Selected_number(String[] function) {
Arrays.fill(function, "");
}
selected_t = Arrays.stream(selected_t_1).collect(Collectors.toList());
selected_t = new ArrayList<>(Arrays.asList(selected_t_1));