gpt4 book ai didi

java | .clear() 函数的问题

转载 作者:行者123 更新时间:2023-12-02 08:52:49 24 4
gpt4 key购买 nike

好吧,这是我第二次在这里发帖,所以如果我提供了太多信息(以及我的代码状态不佳),请原谅我。这是我正在使用的一个较大程序的摘录,但我遇到了一个小问题。 .clear() 函数在奇怪的时间出现错误,我无法查明原因。这里的这个功能旨在根据输入显示相关的性格类型(输入“es”会显示“es”性格类型)。它被设计为接受长度为 0-4 的输入。

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?");
}
}

其中重要的部分是它调用下面的函数并使用上面的数组和列表。相关函数如下:

public void MBTI_Function_2(String function) {
boolean introverted = function.contains("i");
boolean extroverted = function.contains("e");
boolean intuitive = function.contains("n");
boolean sensing = function.contains("s");
boolean thinking = function.contains("t");
boolean feeling = function.contains("f");
boolean judging = function.contains("j");
boolean perceiving = function.contains("p");
if (introverted) {
selected_f.add("Introverted");
}
if (extroverted) {
selected_f.add("Extroverted");
}
if (intuitive) {
selected_f.add("Intuitive");
}
if (sensing) {
selected_f.add("Sensing");
}
if (thinking) {
selected_f.add("Thinking");
}
if (feeling) {
selected_f.add("Feeling");
}
if (judging) {
selected_f.add("Judging");
}
if (perceiving) {
selected_f.add("Perceiving");
}
if (function.isEmpty()) {
return_to_root("\nInvalid Input. Return to Root?");
}
search(function);
}
protected void search(String function) {
for (int i = 0; i < function.length(); i++) {
functions[i] = function.charAt(i);
}
boolean remain = true;
System.arraycopy(types_array, 0, selected_t_1, 0, 16);
if (remain) {
reset_Counter();
for (String type : selected_t_1) {
if (type.contains(String.valueOf(functions[0]))) {
selected_t_2[counter] = type;
counter++;
} else if (functions[0] == '*') {
selected_t = Arrays.asList(selected_t_1);
remain = false;
break;
} else {
continue;
}
}
}
if (remain) {
reset_Counter();
for (String type : selected_t_2) {
if (type.contains(String.valueOf(functions[1]))) {
selected_t_3[counter] = type;
counter++;
} else if (functions[1] == '*') {
selected_t = Arrays.asList(selected_t_2);
remain = false;
break;
} else {
continue;
}
}
}
if (remain) {
reset_Counter();
for (String type : selected_t_3) {
if (type.contains(String.valueOf(functions[2]))) {
selected_t_4[counter] = type;
counter++;
} else if (functions[2] == '*') {
selected_t = Arrays.asList(selected_t_3);
remain = false;
break;
} else {
continue;
}
}
}
if (remain) {
reset_Counter();
for (String type : selected_t_4) {
if (type.contains(String.valueOf(functions[3]))) {
selected_t.add(type);
counter++;
} else if (functions[3] == '*') {
selected_t = Arrays.asList(selected_t_4);
remain = false;
break;
} else {
continue;
}
}
}
}

这当然可以改进和简化,但它确实有效。现在,这些函数要求我在每次完成后重置数组和列表,这就是我遇到问题的地方。

    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, "");
}

具体来说,我在使用reset_Selected_t() 时遇到问题,它使用.clear() 方法。每当输入小于 4 个字符时,我就会收到错误消息。这是解决这个问题的一个非常漫长而迂回的方式,但为什么我在这个清晰的功能上遇到困难呢?它的表兄弟reset_Selected_f() 工作得很好。

最佳答案

您的问题是类似 selected_t = Arrays.asList(selected_t_1);

的行

Arrays.asList 不会根据数组的内容创建新列表;它将现有数组包装在列表中。因此,对列表的更改将反射(reflect)在数组中。

在这种情况下,方法 .clear() 会产生问题,因为它会删除列表中的所有元素。对于内部数组来说这是不可能的;它始终是固定大小。因此,它会抛出异常。

如果您尝试在 Array.asList 创建的列表上使用 addremove,也会发生同样的情况。

关于 java | .clear() 函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60674081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com