gpt4 book ai didi

java - 数组返回为空而不是数字列表

转载 作者:行者123 更新时间:2023-12-02 01:19:06 25 4
gpt4 key购买 nike

基本上,我正在为我的类(class)开发一个程序,从文本文件中查找数字的前导数字并对它们进行排序。我知道我没有最好的编码技能,因为我是初学者,但我知道如何做到这一点。不知何故,我遇到了一个问题,即应该返回的数组为空。实际上,这个数组返回了两次。第一次,到 main 方法,它返回整齐,但是当它返回到 change3() 时,它返回“[].

我尝试更改周围的变量,甚至尝试组合这些方法,但没有任何效果,我只是收到错误。

import java.io.*;
import java.util.*;

class PG
{
public static void main(String[] args)
{
List<String> Strings = new ArrayList<String>();
Strings = change(); // it works perfectly right here
System.out.println(Strings); // it works perfectly right here
List<Integer> Integers = new ArrayList<Integer>();
Integers = change2();
List<Integer> leads = new ArrayList<Integer>();
leads = change3();
}


public static List<String> change()
{
Scanner in = new Scanner(System.in);
List<String> list_S = new ArrayList<String>();
while (in.hasNextLine())
{
String line = in.nextLine();
line = line.replaceAll(",", "");
int result = Integer.parseInt(line);
List<Integer> list_I = new ArrayList<Integer>();
list_I.add(result);
list_S.add(line);
}
return list_S;
}

public static List<Integer> change2()
{
Scanner in = new Scanner(System.in);
List<Integer> list_I = new ArrayList<Integer>();
while (in.hasNextLine())
{
String line = in.nextLine();
line = line.replaceAll(",", "");
int result = Integer.parseInt(line);
List<String> list_S = new ArrayList<String>();
list_I.add(result);
list_S.add(line);
}
return list_I;

}

public static List<Integer> change3()
{
List<String> list = new ArrayList<String>();
list = change(); // This is supposed to give me a list of numbers(string) so it can help me find the leading digit
System.out.println(list); // checking to see if the list even has numbers in it. It does not. It returns [] as if nothing is in the list.
List<Integer> leads = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++)
{
// get the leading integer
}
return leads;
}
}

基本上,现在我只希望它在 change3() 方法中打印列表,这样我就可以继续处理该程序。

最佳答案

试试这个代码。它应该有效。如果它不起作用,请评论该问题。

也将此代码与您的相匹配。

更改方法有一些变化。我已经删除了多余的代码。

如果使用相同的列表,我建议在所有 3 个更改方法中传递该列表。这样您就不必借助扫描仪再次阅读它。

import java.io.*;
import java.util.*;

class PG
{
public static void main(String[] args)
{
List<String> Strings = new ArrayList<String>();
Strings = change(); // it works perfectly right here
System.out.println(Strings); // it works perfectly right here
List<Integer> Integers = new ArrayList<Integer>();
Integers = change2();
List<Integer> leads = new ArrayList<Integer>();
leads = change3();
}


public static List<String> change()
{
Scanner in = new Scanner(System.in);
List<String> list_S = new ArrayList<String>();
while (in.hasNextLine())
{
String line = in.nextLine();
line = line.replaceAll(",", "");
list_S.add(line);
}
return list_S;
}

public static List<Integer> change2()
{
Scanner in = new Scanner(System.in);
List<Integer> list_I = new ArrayList<Integer>();
while (in.hasNextLine())
{
String line = in.nextLine();
line = line.replaceAll(",", "");
int result = Integer.parseInt(line);
list_I.add(result);
}
return list_I;

}

public static List<Integer> change3()
{
List<String> list = new ArrayList<String>();
list = change(); // This is supposed to give me a list of numbers(string) so it can help me find the leading digit
System.out.println(list); // checking to see if the list even has numbers in it. It does not. It returns [] as if nothing is in the list.
List<Integer> leads = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++)
{
// get the leading integer
}
return leads;
}
}

关于java - 数组返回为空而不是数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58043811/

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