Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Python itertools循环函数_Java_Loops - Fatal编程技术网

Java中的Python itertools循环函数

Java中的Python itertools循环函数,java,loops,Java,Loops,Python有一个itertools库,它允许无限循环项目列表 cycle('ABCD') --> A B C D A B C D ... 如何在java中实现相同的功能,但对于数组?例如: int[] a = { 1, 2, 3, 4}; cycle(a) = 1, 2, 3, 4, 1, 2, 3, 4 .... 这个怎么样: public void cycle( int[] a ) { while ( true ) { for ( int val : a

Python有一个itertools库,它允许无限循环项目列表

cycle('ABCD') --> A B C D A B C D ...
如何在java中实现相同的功能,但对于数组?例如:

int[] a = { 1, 2, 3, 4};
cycle(a) = 1, 2, 3, 4, 1, 2, 3, 4 ....
这个怎么样:

public void cycle( int[] a ) {
    while ( true ) {
        for ( int val : a ) {
            ...
        }
    }
}
并通过回调使其有用:

public interface Callback<T> {
    public void execute( T value );
}

public <T> void cycle( T[] a, Callback<T> callback ) {
    while ( true ) {
        for ( T val : a ) {
            callback.execute( val );
        }
    }
}
这个怎么样:

public void cycle( int[] a ) {
    while ( true ) {
        for ( int val : a ) {
            ...
        }
    }
}
并通过回调使其有用:

public interface Callback<T> {
    public void execute( T value );
}

public <T> void cycle( T[] a, Callback<T> callback ) {
    while ( true ) {
        for ( T val : a ) {
            callback.execute( val );
        }
    }
}

如果使用番石榴是一种选择,它已经有了:

 Iterables.cycle

如果使用番石榴是一种选择,它已经有了:

 Iterables.cycle

有趣的是,你也可以制作这样的迭代器

public static void main(String[] args) {
 Integer[] A = new Integer[]{1,2,3};
 CyclicArrayIterator<Integer> iter = new CyclicArrayIterator<>(A);
  for(int i = 0; i < 10; i++){
    System.out.println(iter.next());
  }
}
Guava的方法看起来最干净,但如果您不想包含任何依赖项的话。下面是您可以使用的CyclicIterator类

/**
 * An iterator to loop over an array infinitely.
 */
 public class CyclicArrayIterator<T> implements Iterator<T> {

   private final T[] A;
   private int next_index = 0;

   public CyclicArrayIterator(T[] array){
     this.A = array;
   }

   @Override
   public boolean hasNext() {
     return A[next_index] != null;
   }

   @Override
   public T next() {
     T t = A[next_index % A.length];
     next_index = (next_index + 1) % A.length;
     return t;
   }

   @Override
   public void remove() {
     throw new ConcurrentModificationException();
   }

 }

希望有帮助。

有趣的是,您也可以制作这样的迭代器

public static void main(String[] args) {
 Integer[] A = new Integer[]{1,2,3};
 CyclicArrayIterator<Integer> iter = new CyclicArrayIterator<>(A);
  for(int i = 0; i < 10; i++){
    System.out.println(iter.next());
  }
}
Guava的方法看起来最干净,但如果您不想包含任何依赖项的话。下面是您可以使用的CyclicIterator类

/**
 * An iterator to loop over an array infinitely.
 */
 public class CyclicArrayIterator<T> implements Iterator<T> {

   private final T[] A;
   private int next_index = 0;

   public CyclicArrayIterator(T[] array){
     this.A = array;
   }

   @Override
   public boolean hasNext() {
     return A[next_index] != null;
   }

   @Override
   public T next() {
     T t = A[next_index % A.length];
     next_index = (next_index + 1) % A.length;
     return t;
   }

   @Override
   public void remove() {
     throw new ConcurrentModificationException();
   }

 }

希望有帮助。

这对基本数组本身不起作用,但您可以使用Ints.asList将int[]转换为列表。@LouisWasserman是的,我知道这一点。我刚刚写了一个快速的答案。。谢谢@路易斯瓦瑟曼,天哪!你是番石榴的创造者是的,老师!这对基本数组本身不起作用,但您可以使用Ints.asList将int[]转换为列表。@LouisWasserman是的,我知道这一点。我刚刚写了一个快速的答案。。谢谢@路易斯瓦瑟曼,天哪!你是番石榴的创造者是的,老师!