< prev index next >
src/java.base/share/classes/java/util/Arrays.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -4401,10 +4401,39 @@
@Override
public void sort(Comparator<? super E> c) {
Arrays.sort(a, c);
}
+
+ @Override
+ public Iterator<E> iterator() {
+ return new ArrayItr<>(a);
+ }
+ }
+
+ private static class ArrayItr<E> implements Iterator<E> {
+ private int cursor;
+ private final E[] a;
+
+ ArrayItr(E[] a) {
+ this.a = a;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return cursor < a.length;
+ }
+
+ @Override
+ public E next() {
+ int i = cursor;
+ E[] a = this.a;
+ if (i >= a.length)
+ throw new NoSuchElementException();
+ cursor = i + 1;
+ return a[i];
+ }
}
/**
* Returns a hash code based on the contents of the specified array.
* For any two {@code long} arrays {@code a} and {@code b}
< prev index next >