开发者

Java List type parameter

开发者 https://www.devze.com 2023-01-12 17:24 出处:网络
I have a method which looks like this public void setDayIntervals(Days day, List<HourRange> ranges) {

I have a method which looks like this

public void setDayIntervals(Days day, List<HourRange> ranges) {
        int hourMask = 0;
        for (HourRange intRange : ranges) {
                     int Start= intRange.getStart();
                     int end = intRange.getEnd();
            }
        }

}

I have to pass List of Ranges from another class.

for(int s = 0; s < adSchedule.getTargets().length ; s++ ){
        List<HourRange> ranges = null;
        int Start =  adSchedule.getTargets(s).getStartHour();
        int end =  adSchedule.getTargets(s).getEndHour()-1;
            if(adSchedule.getTargets(s).getDayOfWeek()==DayOfWeek.MONDAY ){
               // ranges ????????? here i have to pass values Start and End 
 开发者_Python百科              CamSchedule.setDayIntervals(Days.ONE, ranges);
             }
    }

Can someone tell me how to pass ranges in the above method setDayIntervals(Days.one, ramges)

public static class HourRange {
        int start;
        int end;

        public HourRange(int start, int end) {
            super();
            if(start > end) 
                throw new IllegalArgumentException();
            this.start = start;
            this.end = end;
        }

        public int getStart() {
            return start;
        }

        public int getEnd() {
            return end;
        }
}


You have to create an HourRange object and add it to the list. Something like this:

 ranges.add(new HourRange(Start, end));

The example assumes that HourRange has this constructor:

 public HourRange(int start, int end) {
   // code to copy start and end to internal fields.
 }

(it has this constructor)


Instead of

List<HourRange> ranges = null;

you probably want

List<HourRange> ranges = new ArrayList<HourRange>();

That gives you a list to add to; until you initialize ranges as a list, nothing can go into that list.

0

精彩评论

暂无评论...
验证码 换一张
取 消