error: cannot find symbol array.add(element);

后端 未结 5 1417
小蘑菇
小蘑菇 2021-01-29 11:34

I have a program that reads from a file, takes each word and adds it to an array as a String. I\'m having a bit of trouble with adding the Strings to the array. I get the e

相关标签:
5条回答
  • 2021-01-29 11:51

    Really long boring answer, skip to bottom if you just want simple answer...

    Ok buddy I think I see your issue here, I am going to start off by saying that you might be confusing STRING LISTS (be careful not to get confused lists can get very complicated and I am getting my masters in which there is an entire class on lists D= ) with STRING ARRAYS.

    A list is a bag that you put various things in, like a grocery bag, you can put in carrots, peas, apples or in our case when programming in java strings, integerss ect... in a bag things do not have an order. An array is a more of like an oreo cookie container (array). You only put oreos in there and they all have go in a slot and stay there, which is unlike a grocery bag (list) where an item can fall to the bottom or the top....

    This is important in an Array you cannot change the size so you cannot do

        array.add(element)
    

    if you find yourself asking why then you need to think about it. What if you make an array that holds 2 elements? Then where does each element go? Even if you do not understand it, the Java language demands that in an Array you specify where objects go. So to add an object to an array you need to specify the location. Then set it equal to w/e you want for example,

    The location,

        array[0];
    

    Simple answer What you are setting it equal to,

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

    End of Simple answer

    Now lets look at the grocery bag (which does not have "slots" like an array) please note that it looks like there is no list in your code,

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

    Once you have made this list THEN you can use the .add() that you used like so,

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

    Now you know the difference good luck. I made this same mistake so many times too...

    0 讨论(0)
  • 2021-01-29 11:51

    You are confused with array and ArrayList.

    Arrays don't have add method, their insertion will be based on indexes

    i.e

    array[index] = value;

    similary they can be retrived only with the index

    i.e

    value = arrray[index];
    
    0 讨论(0)
  • 2021-01-29 11:52

    That's not how you add an element to Java array. Keep a counter outside the loop that will be incremented inside the loop block and then add elements like below:

    int i = 0;
    while(.....){
        .
        .
        array[i] = //element to be added
        i++;
    }
    
    0 讨论(0)
  • 2021-01-29 11:53

    Arrays in java have a static number of slots. You have to know the number you need at the time you instanciate the array.

    What you want is either:

    array[count] = element;
    

    or you can use a List<String> (e.g. ArrayList<String>, since List is just an interface)

    0 讨论(0)
  • 2021-01-29 11:54

    Use an ArrayList instead of an array:

    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);
    }
    
    0 讨论(0)
提交回复
热议问题