错误:找不到符号 array.add(element);

2024-05-06

我有一个程序,它从文件中读取,获取每个单词并将其作为字符串添加到数组中。我在将字符串添加到数组时遇到了一些麻烦。 我收到错误:

SortingWords.java:73: error: cannot find symbol
        array.add(element);
             ^

符号:方法add(String) 位置: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;
}

非常感谢


真的很长无聊的答案,如果你只想简单的答案,请跳到底部...

好吧,伙计,我想我在这里看到你的问题,我首先要说的是你可能会混淆字符串列表(小心不要混淆列表可能会变得非常复杂,我正在获得我的硕士学位,其中有一整个班级在带有字符串数组的列表 D= ) 上。

列表是一个袋子,你可以把各种东西放进去,就像一个杂货袋,你可以放入胡萝卜、豌豆、苹果,或者在我们的例子中,当用 java 字符串、整数等编程时......在袋子里,东西没有顺序。数组更像是奥利奥饼干容器(数组)。你只把奥利奥放进去,它们都会放进一个槽里并留在那里,这与杂货袋(清单)不同,杂货袋(清单)中的物品可能会掉到底部或顶部......

这对于数组来说很重要,你不能改变大小,所以你不能这样做

    array.add(element)

如果您发现自己在问为什么,那么您需要考虑一下。如果创建一个包含 2 个元素的数组怎么办?那么每个元素都去了哪里呢?即使您不理解,Java 语言也要求您在数组中指定对象的去向。因此,要将对象添加到数组中,您需要指定位置。然后将其设置为等于您想要的 w/e 例如,

那个地点,

    array[0];

简单回答你设置它等于什么,

    array[0] = "I want it to equal this string!!!";

简单回答结束

现在让我们看看杂货袋(它没有像数组一样的“槽”)请注意,您的代码中似乎没有列表,

    List<String> myBrandNewShinyList= new ArrayList<String>();

一旦你创建了这个列表,那么你就可以像这样使用你使用的 .add() ,

   myBrandNewShinyList.add("Let's add this string!!!");

现在您知道其中的区别了,祝您好运。我也犯过很多次同样的错误...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:找不到符号 array.add(element); 的相关文章

随机推荐