Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.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 是否有可能使函数像for循环一样工作?_Java_C++ - Fatal编程技术网

Java 是否有可能使函数像for循环一样工作?

Java 是否有可能使函数像for循环一样工作?,java,c++,Java,C++,我想知道是否有任何方法可以编写一个自定义方法作为“for”循环。 由于通过对象的非常不寻常的迭代(好吧,不像典型的迭代那样常见),每次我想使用它时,我都被迫重写它 很抱歉我的英语和非常不清楚的解释,我不是一个非常善于交际的人,但我认为下面的例子将正确地解释我的问题。 下面的代码不是真正的代码,只是一个方案。指针和引用可能会用错,但我的意思是展示我的概念 class Element{ Element *next; // Pointer to the next ob

我想知道是否有任何方法可以编写一个自定义方法作为“for”循环。 由于通过对象的非常不寻常的迭代(好吧,不像典型的迭代那样常见),每次我想使用它时,我都被迫重写它

很抱歉我的英语和非常不清楚的解释,我不是一个非常善于交际的人,但我认为下面的例子将正确地解释我的问题。 下面的代码不是真正的代码,只是一个方案。指针和引用可能会用错,但我的意思是展示我的概念

 class Element{
          Element *next;       // Pointer to the next object
          Element *previous;   // Pointer to the previous object
          // There are also some primitives (int, double etc.) there.

          void actionA(int a){ /* sth happpens to the primitives and may affect *next and *previous */ } 
          void actionB(int b,int c){ /* just like above */ }
          // ............. and even more methods 

    }




void makeActionA(int i){
       Element pointer=start.next;
       while(pointer!=end){
            Element tmp=pointer.next;
            pointer.actionA(i);          //X
            pointer=tmp;
        }
    }

void makeActionBC(int i,int j){
       Element pointer=start.next;
       while(pointer!=end){
            Element tmp=pointer.next;
            pointer.actionB(i,j);            //X
            pointer.actionC(i,j,i*j);        //X
            pointer=tmp;
        }
    }
    // AND SO ON
我们可以看到,makeAction方法的结构几乎相同,除了标有“X”的行之外。 我想把它缩短一些,不要像在“for”循环中那样愚蠢地重复

并用使用自定义循环的更简单方法替换makeAction方法

    void makeActionA(int i){
          Element t,s,e;
          custom_loop(t;s;e){
               t.actionA(i); 
          }
    }

    void makeActionBC(int i,int j){
          Element t,s,e;
          custom_loop(t;s;e){
               t.actionB(i,j);
               t.actionC(i,j,i*j); 
          }
    }
我想到的唯一解决办法是使用元编程和宏的一些神奇技巧。 我不是很喜欢他们,但他们不会吓跑我。 我期待着解决办法。 非常感谢。 对不起,我的英语又错了。

C++,你可以做< /P>
template <typename F>
void makeElementAction(Element& element, F f){
   for (auto* pointer = &element; pointer != nullptr; pointer = pointer->next) {
       f(*pointer);
   }
}
在C++中,你可以做< /P>
template <typename F>
void makeElementAction(Element& element, F f){
   for (auto* pointer = &element; pointer != nullptr; pointer = pointer->next) {
       f(*pointer);
   }
}

在Java中,您可以实现接口
Iterable
,它允许您使用
for
循环迭代“Iterable”结构:

Element start = ...;
for (Element current : start) {
   // do something with current element
}

class Element implements Iterable<Element> {
    ...

    public Iterator<Element> iterator() {
        return new ElementIterator(this);
    }
}

class ElementIterator implements Iterator<Element> {
    private Element current;

    ElementIterator(Element start) {
        this.current = start;
    }

    public boolean hasNext() {
        return current != null;
    }

    public Element next() {
        Element result = current;
        current = current.next;
        return result;
    }
}
元素开始=。。。;
用于(当前元素:开始){
//对当前元素执行某些操作
}
类元素实现了Iterable{
...
公共迭代器迭代器(){
返回新的元素迭代器(this);
}
}
类ElementIterator实现迭代器{
私有元件电流;
元素迭代器(元素开始){
这个。电流=启动;
}
公共布尔hasNext(){
返回电流!=null;
}
公共元素next(){
元素结果=当前;
当前=当前。下一步;
返回结果;
}
}

在Java中,您可以实现接口
Iterable
,它允许您使用
for
循环迭代“Iterable”结构:

Element start = ...;
for (Element current : start) {
   // do something with current element
}

class Element implements Iterable<Element> {
    ...

    public Iterator<Element> iterator() {
        return new ElementIterator(this);
    }
}

class ElementIterator implements Iterator<Element> {
    private Element current;

    ElementIterator(Element start) {
        this.current = start;
    }

    public boolean hasNext() {
        return current != null;
    }

    public Element next() {
        Element result = current;
        current = current.next;
        return result;
    }
}
元素开始=。。。;
用于(当前元素:开始){
//对当前元素执行某些操作
}
类元素实现了Iterable{
...
公共迭代器迭代器(){
返回新的元素迭代器(this);
}
}
类ElementIterator实现迭代器{
私有元件电流;
元素迭代器(元素开始){
这个。电流=启动;
}
公共布尔hasNext(){
返回电流!=null;
}
公共元素next(){
元素结果=当前;
当前=当前。下一步;
返回结果;
}
}

我发现了一个有趣的结构。 它也解决了这个问题

循环看起来:

public static void loop(Element start, Element end, T t){
         for (Element pointer = start; pointer != end; pointer = pointer.next) {
              t.run(pointer);
          }
}
interface T{
    void run(Element pointer);
 }
新的方法看起来是这样的

void makeActionA(int i){
     loop(s,e,(pointer)-> pointer.actionA(i) );
 }

void makeActionBC(int i,int j){
     loop(s,e,(pointer)->{pointer.actionB(i,j); pointer.actionC(i,j,i*j);} );
 }
链接:

我发现了一个有趣的结构。 它也解决了这个问题

循环看起来:

public static void loop(Element start, Element end, T t){
         for (Element pointer = start; pointer != end; pointer = pointer.next) {
              t.run(pointer);
          }
}
interface T{
    void run(Element pointer);
 }
新的方法看起来是这样的

void makeActionA(int i){
     loop(s,e,(pointer)-> pointer.actionA(i) );
 }

void makeActionBC(int i,int j){
     loop(s,e,(pointer)->{pointer.actionB(i,j); pointer.actionC(i,j,i*j);} );
 }
链接:

您正在查找lambda表达式。请查看:@silenetemplar请不要同时标记为和。我猜你的意思只是后者,但我不能确定。你在寻找lambda表达式。请看:@silenetemplar请不要同时标记为和。我怀疑你只是指后者,但我不能确定。你能写一个使用它的元素类的例子吗?当然,我跳过了ElementIterator#删除实现,在这个例子中它是无关的。你能写一个使用它的元素类的例子吗?当然,我跳过了ElementIterator#删除实现,在本例中它是不相关的谢谢,但是你能解释[&](元素和元素)部分吗?它是一个带捕获的lambda(对于
i
)。谢谢,但是你能解释[&](元素和元素)部分吗?它是一个带捕获的lambda(对于
i
)。