How do you write these loops better in Java?

I have a set of objects on which I would like to perform some operations, in the order in which they are repeated. After the operation will be called on them, I would like to perform other operations on them. Basically, the code will look something like this:

for(int i = 0;i < myobj.size();i++)
{
   myobj.at(i).doSomething();
}

for(int i = 0;i < myobj.size();i++)
{
   myobj.at(i).doSomethingElse();
}

It looks ugly to me. How can I rewrite this into something better? The order of operations must remain unchanged.

+3
source share
10 answers

I don't know what myobj is, but if it is Iterable, you can use the foreach loop:

for (Foo foo : myobj) {
  foo.doSomething();
}

for (Foo foo : myobj) {
  foo.doSomethingElse();
}

If this is not Iterable, then this can help another code.

+24
source

, , :

createData();
saveData();
+6

foreach:

for (MyObject currentObject : myobj) {
  currentObject.doSomething();
}

for (MyObject currentObject : myobj) {
  currentObject.doSomethingElse();
}
+4

.

+3

lambdaj, , , :

forEach(myobj).doSomething();
forEach(myobj).doSomethingElse();
+2

: (, null):

while ((holder = holder.doSomething())!=null);
while ((holder = holder.doSomethingElse())!=null);

, . , Iterable ; Iterator myObj:

while (myObj.hasNext()) myObj.next().doSomething();
myObj.resetIterator();
while (myObj.hasNext()) myObj.next().doSomethingElse();

- - , doSomething() doSomethingElse() , .

for, - ( , ):

int size = myObj.size(), i=0;
while (i<size) myObj.at(i++).doSomething();
i=0;
while (i<size) myObj.at(i++).doSomethingElse();

, doSomething() doSomethingElse() (.. , ), , .

int size = myObj.size(), i=0;
while (i<size) myObj.at(i++).doSomething();
while (i>0) myObj.at(--i).doSomethingElse();

, , Iterator , myObj ListIterator Iterator (, ... ).

+1

- ,

bool d=false;
for(int i = 0;i < myobj.size();i++)
{
   if(d==false){
     myobj.at(i).doSomething();
   }else{
     myobj.at(i).doSomethingElse();
   }
   if(i==myobj.size()-1 && d==false){ d=true; i=0;}
}
0

.

, , , , ( ), , , - .

, , ( Java-) .

public class IterationSample {
    public static void main( String [] args ) {

        Foo [] items = new Foo[0]; // get it from somewhere  ....

        iterate( items , new _(){void with( Foo f ){
            f.doSomething();
        }});    

        iterate( items , new _(){void with( Foo f ){
            f.doSomethingElse();
        }});    

        iterate( items , new _(){void with( Foo f ){
            f.doBar();
        }});   



    }
    // write  the loop once.
    static void iterate( Foo [] items, _ visitor ) {
        for( Foo f : items ) {
            visitor.with( f );
        }
    }
}

Foo , .

// Not really abstract just for the sake of the sample. Use your own.
abstract class Foo {
    abstract void doSomething();
    abstract void doSomethingElse();
    abstract void doBar();

}

, ""

// made abstract instead of interface just to avoid having to type 
// "public" each time. 
abstract class  _ {
    abstract void with( Foo f );
}

, .

         iterate( items ,new _(){ with( Foo f ){ 

:

"noise" foo

, ?

, :

 _ [] operations = {
        new _(){void with( Foo f ){
            f.doSomething();
        }},
        new _(){void with( Foo f ){
            f.doSomethingElse();
        }},
        new _(){void with( Foo f ){
            f.doBar();
        }}
 };

:)

  for( _ andPerformAction : operations ) {
      iterate( items , andPerformAction );
  }

, = -S

0

ummm, ?

for(int i = 0;i < myobj.size();i++)
{   
    myobj.at(i).doSomething();
    myobj.at(i).doSomethingElse();
}
-3

Source: https://habr.com/ru/post/1721556/


All Articles