Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8157110

Behavioural change with extended ArrayList class

XMLWordPrintable

      ArrayList Iterator behavior has been changed from jdk6 to jdk7.

      Create and compile the below 3 class and then run Main.java.
      Using JDK6 it prints the element but JDK7 no element is printed.

      // DynamicResult.java
      public class DynamicResult {
      public Object next() {
      return Math.random() * ( 1000 - 1 );
      }
      }


      // DynamicArrayList.java
      import java.util.ArrayList;

      public class DynamicArrayList extends ArrayList{

      private int size;
      private DynamicResult dynValues;

      public DynamicArrayList(DynamicResult result, int size){
      this.dynValues = result;
      this.size = size;
      }
          
      @Override
      public int size() {
      return super.size() + size;
      }

      @Override
      public Object get(int index) {
      if(index < size){
      return dynValues.next();
      } else {
      return super.get(index - size);
      }
      }
      }

      // Main.java
      import java.util.ArrayList;
      import java.util.Iterator;

      public class Main {
      public static void main(String [] args){
      System.out.println("This code does not work in JDK7 but works in JDK6");
      System.out.println("Print 1 element as size is 1");
      ArrayList list = new DynamicArrayList(new DynamicResult(), 1);

      Iterator iter = list.iterator();
      if(iter !=null && iter.hasNext()){
      System.out.println(iter.next());
      }
      }
      }

            rpatil Ramanand Patil (Inactive)
            shadowbug Shadow Bug
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: