简体   繁体   中英

Sum Method / for-loop

It may be a stupid question for you, but I´m pretty much of a scrub when it comes to java. I want write a method public int sum() which is going through my list and .

I dont get what I´m supposed to write in my for-loop. I tried "length" and so on, but it wasn´t working for a reason.

      import java.util.*;
          public class VisitorList {
                BesucherTag head;
                BesucherTag z;

            public class VisitorDay {
            public int day;
            public int month;
            public int year;    
            public int number;
            VisitorDay next = null;

            public VisitorDay(int day, int month, int year, int number){
                this.day = day;
                this.month = month;
                this.year = year;
                this.number = number;
            }
    }

                public VisitorList(){
                head = new VisitorDay(0,0,0,0);
                z = new VisitorDay(0,0,0,0);
                head.next = z;
                z.next = z;
                }

                public int sum(){
                    for(int i = 0; i <= "?!" ; i++){

                    }
}

It looks like you're trying to create your own linked list implementation called VisitorList . You should probably just use java.util.LinkedList<VisitorDay> . Then you would traverse the list:

java.util.List<VisitorDay> listOfVisitors = new java.util.LinkedList<VisitorDay>();

//code which adds your visitors

//now the loop
for(VisitorDay vd : listOfVisitors){
    count++;
}

Or something along these lines.

If you need to implement you own LinkedList, you'll want something like this. If you cant use Lists, then use an array. that's how Lists are implemented under the hood. You'll also need to implement some sort of addVisitorDay method that adds to the dayList and auto-increases in size if the capacity is reached.

The sum method should go in the VisitorList class. The class contains an array of VisitorDay s and that's where you get the sum from.

public class VisitorList {

    public static final CAPACITY = 10;
    VisitorDay[] dayList = new VisitorDay[CAPACITY];  

    public int sum(){
        int sum = 0;
        for (VisitorDay vd : dayList){
            sum += vd.getNumber();
        }
        return sum;
    }  
}

public class VisitorDay{
    private number;

    public getNumber(){
        return number;
    }
}

public class VisitorDay {
        public int day;
        public int month;
        public int year;
        public int number;    

        public static sum = 0;

        public VisitorDay(int day, int month, int year, int number){
            this.day = day;
            this.month = month;
            this.year = year;
            this.number = number;
            sum += number;
        }

        public int sum(){
            return sum;
        }
}

Since the field sum is static everytime you create a VisitorDay object, the sum will increase. Whenever you want to get the sum, just call the sum() method and you will have your sum.

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