error: cannot find symbol array.add(element);
error: cannot find symbol array.add(element);
我有一个从文件中读取的程序,获取每个单词并将其作为字符串添加到数组中。我在将字符串添加到数组时遇到了一些麻烦。
我收到错误:
SortingWords.java:73: error: cannot find symbol
array.add(element);
^
符号:方法添加(字符串)
location:String[]
类型的变量数组
我检查了我的拼写,似乎一切正常。
发生了什么事,我该如何解决?
public class SortingWords {
//global variables
public static int count = 0;
public static void main(String [ ] commandlineArguments){
String[ ] array = readFileReturnWords(commandlineArguments[0]);
sortAndPrintArray(array, commandlineArguments[0]);
if (commandlineArguments.length != 1) {
System.out.println("Please enter the INPUT file name as the 1st commandline argument.");
System.out.println("Please enter exactly one (1) commandline arguments.");
// Immediately terminates program
System.exit(1);
}// end of if
// if no commandline argument errors, continue program
String inputFile = commandlineArguments[0];
}
/**
* readFileReturnWords - Used To Read from File & Store Each Word in Array
*
* @param inputFile is the name of the file
*/
public static String [] readFileReturnWords(String inputFile){
//create array
//read one word at a time from file and store in array
//return the array
//error checking for commandline input
//make an array of Strings
final Integer MAX = 100;
String array[] = new String [MAX];
String element = "";
// read items from file & store in array
//connects to file
File file = new File(inputFile);
Scanner scanFile = null;
try {
scanFile = new Scanner(file);
}
catch (FileNotFoundException exception) {
// Print error message.
System.out.print("ERROR: File not found for " + inputFile);
}
// if made connection to file, read from file
if (scanFile != null) {
String firstLineOfFile = "";
firstLineOfFile = scanFile.nextLine();
// keeps looping if file has more lines..
while (scanFile.hasNextLine()) {
// get a line of text..
String line = scanFile.nextLine();
// divides each line by commas
Scanner lineInput = new Scanner(line).useDelimiter("\s*");
element = lineInput.next();
array.add(element);
count ++;
}
}
System.out.println("Alphabetical (ASCII) listing of words in file: ");
System.out.println("Index Element");
for(int i=0;i<MAX;i++){
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(array[i]);
}
return array;
}
非常感谢
你对数组和 ArrayList
感到困惑。
数组没有add方法,它们的插入将基于索引
即
array[index] = value;
类似的,它们只能通过索引检索
即
value = arrray[index];
使用 ArrayList
而不是数组:
List<String> array = new ArrayList<String>();
while (scanFile.hasNextLine()) {
String line = scanFile.nextLine();
Scanner lineInput = new Scanner(line).useDelimiter("\s*");
element = lineInput.next();
// the add() method is available for Collections but
// not for primitive arrays
array.add(element);
count++;
}
for (String element : array) {
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(element);
}
java 中的数组具有固定数量的槽。你必须在实例化数组时知道你需要的数字。
你想要的是:
array[count] = element;
或者您可以使用 List<String>
(例如 ArrayList<String>
,因为 List
只是一个 interface
)
这不是向 Java 数组添加元素的方式。在循环外保留一个计数器,它将在循环块内递增,然后添加如下元素:
int i = 0;
while(.....){
.
.
array[i] = //element to be added
i++;
}
很长很无聊的回答,如果你只想简单回答就跳到底部...
好的,伙计,我想我在这里看到了你的问题,我首先要说你可能会混淆字符串列表(注意不要混淆列表会变得非常复杂,我正在让我的主人在其中列表 D= 上有一个完整的 class ) 带有 STRING ARRAYS。
列表是一个袋子,你可以把各种东西放进去,比如杂货袋,你可以放入胡萝卜、豌豆、苹果,或者在我们的例子中,当在 java 字符串、整数等中编程时......包里的东西没有顺序。数组更像是一个奥利奥饼干容器(数组)。你只需要把奥利奥放进去,它们都会放进一个槽里并留在那里,这不像杂货袋(清单),里面的东西可以掉到底部或顶部....
这在数组中很重要,你不能改变大小,所以你不能做
array.add(element)
如果您发现自己在问为什么,那么您需要考虑一下。如果您创建一个包含 2 个元素的数组怎么办?那么每个元素去哪里了?即使您不理解,Java 语言也要求您在数组中指定对象的位置。因此,要将对象添加到数组中,您需要指定位置。然后设置它等于你想要的 w/e 例如,
地点,
array[0];
简单回答
您将其设置为什么,
array[0] = "I want it to equal this string!!!";
简单回答结束
现在让我们看看杂货袋(它没有像数组一样的 "slots")请注意,您的代码中似乎没有列表,
List<String> myBrandNewShinyList= new ArrayList<String>();
一旦你列出了这个列表,那么你就可以像这样使用你使用的 .add(),
myBrandNewShinyList.add("Let's add this string!!!");
现在你知道区别了,祝你好运。同样的错误我也犯过很多次了...
我有一个从文件中读取的程序,获取每个单词并将其作为字符串添加到数组中。我在将字符串添加到数组时遇到了一些麻烦。 我收到错误:
SortingWords.java:73: error: cannot find symbol
array.add(element);
^
符号:方法添加(字符串) location:String[]
类型的变量数组我检查了我的拼写,似乎一切正常。 发生了什么事,我该如何解决?
public class SortingWords {
//global variables
public static int count = 0;
public static void main(String [ ] commandlineArguments){
String[ ] array = readFileReturnWords(commandlineArguments[0]);
sortAndPrintArray(array, commandlineArguments[0]);
if (commandlineArguments.length != 1) {
System.out.println("Please enter the INPUT file name as the 1st commandline argument.");
System.out.println("Please enter exactly one (1) commandline arguments.");
// Immediately terminates program
System.exit(1);
}// end of if
// if no commandline argument errors, continue program
String inputFile = commandlineArguments[0];
}
/**
* readFileReturnWords - Used To Read from File & Store Each Word in Array
*
* @param inputFile is the name of the file
*/
public static String [] readFileReturnWords(String inputFile){
//create array
//read one word at a time from file and store in array
//return the array
//error checking for commandline input
//make an array of Strings
final Integer MAX = 100;
String array[] = new String [MAX];
String element = "";
// read items from file & store in array
//connects to file
File file = new File(inputFile);
Scanner scanFile = null;
try {
scanFile = new Scanner(file);
}
catch (FileNotFoundException exception) {
// Print error message.
System.out.print("ERROR: File not found for " + inputFile);
}
// if made connection to file, read from file
if (scanFile != null) {
String firstLineOfFile = "";
firstLineOfFile = scanFile.nextLine();
// keeps looping if file has more lines..
while (scanFile.hasNextLine()) {
// get a line of text..
String line = scanFile.nextLine();
// divides each line by commas
Scanner lineInput = new Scanner(line).useDelimiter("\s*");
element = lineInput.next();
array.add(element);
count ++;
}
}
System.out.println("Alphabetical (ASCII) listing of words in file: ");
System.out.println("Index Element");
for(int i=0;i<MAX;i++){
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(array[i]);
}
return array;
}
非常感谢
你对数组和 ArrayList
感到困惑。
数组没有add方法,它们的插入将基于索引
即
array[index] = value;
类似的,它们只能通过索引检索
即
value = arrray[index];
使用 ArrayList
而不是数组:
List<String> array = new ArrayList<String>();
while (scanFile.hasNextLine()) {
String line = scanFile.nextLine();
Scanner lineInput = new Scanner(line).useDelimiter("\s*");
element = lineInput.next();
// the add() method is available for Collections but
// not for primitive arrays
array.add(element);
count++;
}
for (String element : array) {
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(element);
}
java 中的数组具有固定数量的槽。你必须在实例化数组时知道你需要的数字。
你想要的是:
array[count] = element;
或者您可以使用 List<String>
(例如 ArrayList<String>
,因为 List
只是一个 interface
)
这不是向 Java 数组添加元素的方式。在循环外保留一个计数器,它将在循环块内递增,然后添加如下元素:
int i = 0;
while(.....){
.
.
array[i] = //element to be added
i++;
}
很长很无聊的回答,如果你只想简单回答就跳到底部...
好的,伙计,我想我在这里看到了你的问题,我首先要说你可能会混淆字符串列表(注意不要混淆列表会变得非常复杂,我正在让我的主人在其中列表 D= 上有一个完整的 class ) 带有 STRING ARRAYS。
列表是一个袋子,你可以把各种东西放进去,比如杂货袋,你可以放入胡萝卜、豌豆、苹果,或者在我们的例子中,当在 java 字符串、整数等中编程时......包里的东西没有顺序。数组更像是一个奥利奥饼干容器(数组)。你只需要把奥利奥放进去,它们都会放进一个槽里并留在那里,这不像杂货袋(清单),里面的东西可以掉到底部或顶部....
这在数组中很重要,你不能改变大小,所以你不能做
array.add(element)
如果您发现自己在问为什么,那么您需要考虑一下。如果您创建一个包含 2 个元素的数组怎么办?那么每个元素去哪里了?即使您不理解,Java 语言也要求您在数组中指定对象的位置。因此,要将对象添加到数组中,您需要指定位置。然后设置它等于你想要的 w/e 例如,
地点,
array[0];
简单回答 您将其设置为什么,
array[0] = "I want it to equal this string!!!";
简单回答结束
现在让我们看看杂货袋(它没有像数组一样的 "slots")请注意,您的代码中似乎没有列表,
List<String> myBrandNewShinyList= new ArrayList<String>();
一旦你列出了这个列表,那么你就可以像这样使用你使用的 .add(),
myBrandNewShinyList.add("Let's add this string!!!");
现在你知道区别了,祝你好运。同样的错误我也犯过很多次了...