从ArrayList更改为Java字符串数组
我有一个java方法,它返回一个数组列表,但我想返回一个字符串数组。该方法读取words.txt文件(包含每行有一个单词的所有单词),我想将这些单词存储到字符串数组中 以下是我已有的代码:从ArrayList更改为Java字符串数组,java,arrays,string,oop,arraylist,Java,Arrays,String,Oop,Arraylist,我有一个java方法,它返回一个数组列表,但我想返回一个字符串数组。该方法读取words.txt文件(包含每行有一个单词的所有单词),我想将这些单词存储到字符串数组中 以下是我已有的代码: public static ArrayList<String> readFile(){ File myFile=new File("./src/folder/words.txt"); Scanner s1=null; //Creates ArrayList to stor
public static ArrayList<String> readFile(){
File myFile=new File("./src/folder/words.txt");
Scanner s1=null;
//Creates ArrayList to store each String aux
ArrayList<String> myWords = new ArrayList<String>();
try {
s1 = new Scanner(myFile);
}catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
}
while(s1.hasNext()){
String aux=s1.next();
System.out.println(aux);
}
s1.close();
return myWords;
}
publicstaticarraylistreadfile(){
File myFile=新文件(“./src/folder/words.txt”);
扫描程序s1=空;
//创建ArrayList以存储每个字符串
ArrayList myWords=新建ArrayList();
试一试{
s1=新扫描仪(myFile);
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
e、 printStackTrace();
}
while(s1.hasNext()){
字符串aux=s1.next();
系统输出打印项次(辅助);
}
s1.关闭();
返回我的话;
}
是否可以更改此代码以返回字符串[]?您可以调用以将列表
转换为字符串[]
。与显式关闭扫描器
和列表
界面相比,我更喜欢尝试使用资源
。大概
public static String[] readFile() { // <-- You could pass File myFile here
File myFile = new File("./src/folder/words.txt");
// Creates ArrayList to store each String aux
List<String> myWords = new ArrayList<>();
try (Scanner s1 = new Scanner(myFile)) {
while (s1.hasNext()) {
String aux = s1.next();
System.out.println(aux);
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
}
return myWords.toArray(new String[0]);
}
publicstaticstring[]readFile(){//您可以调用将列表
转换为字符串[]
。我还希望尝试使用资源
而不是显式关闭扫描仪
和列表
接口。例如
public static String[] readFile() { // <-- You could pass File myFile here
File myFile = new File("./src/folder/words.txt");
// Creates ArrayList to store each String aux
List<String> myWords = new ArrayList<>();
try (Scanner s1 = new Scanner(myFile)) {
while (s1.hasNext()) {
String aux = s1.next();
System.out.println(aux);
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
}
return myWords.toArray(new String[0]);
}
publicstaticstring[]readFile(){/最后添加以下内容:
String [] arr = myWords.toArray(new String[myWords.size()]);
return arr;
或者简单地说
return myWords.toArray(new String[myWords.size()]);
最后加上:
String [] arr = myWords.toArray(new String[myWords.size()]);
return arr;
或者简单地说
return myWords.toArray(new String[myWords.size()]);
尝试使用collections类的内置函数
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("x");
stringList.add("y");
stringList.add("z");
stringList.add("a");
/*ArrayList to Array Conversion */
/*You can use the toArray method of the collections class and pass the new String array object in the constructor while making a new String array*/
String stringArray[]=stringList.toArray(new String[stringList.size()]);
for(String k: stringArray)
{
System.out.println(k);
}
ArrayList stringList=新建ArrayList();
stringList.添加(“x”);
字符串列表。添加(“y”);
stringList.添加(“z”);
stringList.添加(“a”);
/*ArrayList到Array的转换*/
/*您可以使用collections类的toArray方法,在创建新的字符串数组时在构造函数中传递新的字符串数组对象*/
String stringArray[]=stringList.toArray(新字符串[stringList.size()]);
for(字符串k:stringArray)
{
系统输出println(k);
}
尝试使用collections类的内置函数
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("x");
stringList.add("y");
stringList.add("z");
stringList.add("a");
/*ArrayList to Array Conversion */
/*You can use the toArray method of the collections class and pass the new String array object in the constructor while making a new String array*/
String stringArray[]=stringList.toArray(new String[stringList.size()]);
for(String k: stringArray)
{
System.out.println(k);
}
ArrayList stringList=新建ArrayList();
stringList.添加(“x”);
字符串列表。添加(“y”);
stringList.添加(“z”);
stringList.添加(“a”);
/*ArrayList到Array的转换*/
/*您可以使用collections类的toArray方法,在创建新的字符串数组时在构造函数中传递新的字符串数组对象*/
String stringArray[]=stringList.toArray(新字符串[stringList.size()]);
for(字符串k:stringArray)
{
系统输出println(k);
}
“我可以更改此代码以返回字符串[]吗?”-可以,但是。在声明数组之前,您需要知道要返回的数字行。或者,您可以将内容读入数组列表
,然后使用它创建字符串
的新数组(因为您将知道需要多少元素)。另一方面。你不应该引用src
,一旦编译并导出程序,它就不存在了。你也不应该从s1
读取数据。如果出现错误,这将创建一个NullPointerException
看到这一点,我知道行数,所以我想我可以这样做!我应该这样做吗创建对象字符串数组?使用大小之类的东西?或者您建议如何创建?您可以编写代码吗?这可能会有很大帮助。“我可以更改此代码以返回字符串[]吗?”-是的,但是。在声明数组之前,您需要知道要使用的数字行。或者,您可以将内容读入ArrayList
,然后使用它创建一个新的字符串数组(因为您将知道需要多少个元素)。另一方面。你不应该引用src
,一旦编译并导出程序,它就不存在了。你也不应该从s1
读取数据。如果出现错误,这将创建一个NullPointerException
看到这一点,我知道行数,所以我想我可以这样做!我应该这样做吗创建对象字符串数组?大小和那些东西?或者你建议怎么做?你能编写代码吗?这可能会有很大帮助。我正在导入:import java.awt.List;但是第5行“List”中有一个错误@Pedroimport java.util.List;
-请点击我答案中的JavaDoc链接。我正在导入:import java.awt.List;但是第5行“List”@Pedroimport java.util.List;
-请点击我答案中的JavaDoc链接。