简体   繁体   中英

Java varargs to require indefinite number of variables

I am trying to apply varargs. I have declared a method which requires an indefinite amount of variables like this:

private Subject carMonitor;

public AdvancedMonitor(Subject ... carMonitors){
    for (Subject carMonitor : carMonitors){
    this.carMonitor = carMonitor;
    carMonitor.registerObserver(this);
    }
}

However, when I try to call it in my main method, I am not able to use anything other than one argument:

    BigCar bigCar = new BigCar();
    SmallCar smallCar = new SmallCar();
    AdvancedMonitor doubleAdvancedDisplay1 = new AdvancedMonitor();
    AdvancedMonitor doubleAdvancedDisplay2 = new AdvancedMonitor(bigCar);
    AdvancedMonitor doubleAdvancedDisplay3 = new AdvancedMonitor(bigCar, smallCar);

Only the second one works. Why is this?

在此处输入图片说明

Is it related to my interface?

public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}

big car interface -- small car is pretty much the same for now :

public class BigCar implements Subject {
    private ArrayList observers;
    private int state;

    public BigCar(){
        observers = new ArrayList();
    }

    public void registerObserver(Observer o){
        observers.add(o);
    }

    public void removeObserver(Observer o){
        int i = observers.indexOf(o);
        if (i >= 0){
            observers.remove(i);
        }
    }

    public void notifyObservers(){
        for (int i = 0; i < observers.size(); i++){
            Observer observer = (Observer)observers.get(i);
            observer.update(state);
        }
    }

    public void stateChanged() {
        notifyObservers();
    }

    public void setState(int state){
        this.state = state;
        stateChanged();
    }
}

I write following code:

public class Test {
    public static class AdvancedMonitor {
        private String carMonitor;

        public AdvancedMonitor(String... carMonitors) {
            for (String carMonitor : carMonitors) {
                this.carMonitor = carMonitor;
                System.out.println(this.carMonitor);
            }
        }
    }

    public static void main(String[] args) {
        String bigCar = "bigCar";
        String smallCar = "smallCar";
        System.out.println("step 1");
        AdvancedMonitor doubleAdvancedDisplay1 = new AdvancedMonitor();
        System.out.println();
        System.out.println("step 2");
        AdvancedMonitor doubleAdvancedDisplay2 = new AdvancedMonitor(bigCar);
        System.out.println();
        System.out.println("step 3");
        AdvancedMonitor doubleAdvancedDisplay3 = new AdvancedMonitor(bigCar, smallCar);
    }
}

And I have following result:

step 1

step 2
bigCar

step 3
bigCar
smallCar

In my opinion, all correct. What is wrong in your case? Do you use logging or System.out.println to debug your problem? It's look like your problem isn't with Java varagrs, but you have some exception in carMonitor.registerObserver(this) .

PS Also, you understand that every AdvancedMonitor has only a one varible carMonitor? And using new AdvancedMonitor(bigCar, smallCar); in result you have AdvancedMonitor only with smallCar in private String carMonitor; ?

PPS Also bad idea to use this in construstor, because object isn't really create when running construstor.

Actually the Constructor works.

Please check these statements:
SmallCar and BigCar both implements Subject
class AdvancedMonitor implements Observer
AdvancedMonitor doubleAdvancedDisplay is not declared several times but in your code it is. It should be smth like:

AdvancedMonitor doubleAdvancedDisplay1 = new AdvancedMonitor();
AdvancedMonitor doubleAdvancedDisplay2 = new AdvancedMonitor(bigCar);
AdvancedMonitor doubleAdvancedDisplay3 = new AdvancedMonitor(bigCar, smallCar);

I hope it'll help you

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