What will be the output of the following Java program? [code] public class BoxDemo { public static <U> void addBox(U u,

Hoàng Minh An

New member
What will be the output of the following Java program?
Code:
    public class BoxDemo
    {
        public static <U> void addBox(U u, 
           java.util.List<Box<U>> boxes)
          {
            Box<U> box = new Box<>();
            box.set(u);
            boxes.add(box);
          }
        public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
        {
            int counter = 0;
            for (Box<U> box: boxes)
            {
                U boxContents = box.get();
                System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
                counter++;
            }
        }
        public static void main(String[] args)
        {
            java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
            BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
            BoxDemo.outputBoxes(listOfIntegerBoxes);
        }
    }
A. 10
B. Box #0 [10]
C. Box contains [10]
D. Box #0 contains [10]
 

Tìm kiếm

Back
Top