-
Bug
-
Resolution: Won't Fix
-
P3
-
None
-
7, 8, 9
-
generic
-
generic
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());
}
}
}
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());
}
}
}
- relates to
-
JDK-8158160 Change in behaviour of ArrayList iterator between JDK 6 and JDK 7
-
- Closed
-