简体   繁体   中英

.add() method not working for ArrayList

I'm learning Java and I'm stuck with a little problem. The method .add() applied to my ArrayList object doesn't want to compile. I tried the same thing (with different parameters) on another program and it is working fine, so there's clearly something I've done wrong this time, but I can't figure out what. Any help would be appreciated.

import java.util.List;
import java.util.ArrayList;
private List<String> science = new ArrayList<String>();
science.add("geography");

This bit of code gives me the following error:

  • Syntax error, insert ")" to complete MethodDeclaration
  • Syntax error, insert "SimpleName" to complete QualifiedName
  • Syntax error on token ".", @ expected after this token
  • Syntax error, insert "Identifier (" to complete MethodHeaderName

This is the entire code of the program:

import java.util.ArrayList;
import java.util.List;

public class Facolta {

        private String nome;
        private int facilities, tipo;
        private List<String> science = new ArrayList<String>();
        private List<String> other = new ArrayList<String>();

        science.add("geography");


        public Facolta(String nome, int facilities, int tipo) {
            this.nome = nome;
            if (science.toString().contains(nome)) {
                this.tipo = 2;
            }
            else if (other.toString().contains(nome)) {
                this.tipo = 1;
            }
            this.facilities = facilities;
        }

        public String getNome() {
            return nome;
        }

        public void setNome(String nome) {
            this.nome = nome;
        }

        public void getFacilities() {
            if (tipo == 1)
                System.out.println("Lab number: " + facilities);
            else if (tipo == 2)
                System.out.println("Library number: " + facilities);
            else
                System.err.println("Facility not available.");

        }

        public void setFacilities(int facilities) {
            if (facilities < 3) this.facilities = facilities;
            else System.err.println("Facility not specified.");
        }

}

You can't just put any kind of code in any place of your class.

In this case, you could move that line into your constructor for example.

You could also use an init block; so just put braces

 { 
   sciences.add("blocked");
 }

right there (see here for details on that).

But the real thing is: I guess that science list contains the subjects some student is supposed to take. So it should be rather a parameter of your constructor; instead of hardcoding that string within your source code!

The alternative would be to make that list a real "constant"; like this:

private final List<String> SCIENCES_THAT_GIVE_TIPO =  Arrays.asList("geography", "whatever");

to make your intention clearer!

And beyond that: using Strings to denote such things is of course simple and straight forward; but a rather bad design. So, just in case you want to play around a bit; you might want to read about Java enums ; and then consider to turn your different sciences into an Enum!

anyway,

List<String> science  = Arrays.asList("geography", "geography2");

This works for your condition

The syntax is incorrect.

science.add("geography") - should be done in a method or in a constructor.

我对 ArrayList 的建议

ArrayList<String> science =new ArrayList<>(Arrays.asList("geography", "geography2"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM