Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

meaning of for loop and multiple getters

Hi,
I see for loop like this

for(LineOfBusiness bus: getDomain().getPart().getLines()){
if (AB.equals(bus.getCd().getOherCode())){
}

What is meaning of above for loop. I do not see initialization , condition, increment .

I see code like above quite often where they have of chain of getter methods and lose track of what object they are going against and which one they are finally getting. Is it is a good idea to draw a diagram using pen and pencil or write hierarchy with some arrows to identify super and sub classes, interfaces etc to see what finally coming out as return esp when you are looking code for the first time to fix some defects without much documentation. Are there any kind of real time practical 'has-a, is-a' kind of examples dealing with interfaces and classes preferably with chain of getters like 5, 10 of them. please advise
SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

I was reading below link on similar lines of thoughts

http://www.javapractices.com/topic/TopicAction.do?Id=196

I did not understand this part

 //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
By the way the excel cell value example is very good and practical. thank you
Avatar of gudii9

ASKER

import java.util.*;
import java.math.BigDecimal;

public final class ForEachExamples {

  public static void main(String... aArgs){
    List<Number> numbers = new ArrayList<>();
    numbers.add(new Integer(42));
    numbers.add(new Integer(-30));
    numbers.add(new BigDecimal("654.2"));

    //typical for-each loop
    //processes each item, without changing the collection or array.
    for (Number number : numbers){
      log(number);
    }

    //use with an array
    String[] names = {"Ethan Hawke", "Julie Delpy"};
    for(String name : names){
      log("Name : " + name);
    }

    //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
    Collection<String> words = new ArrayList<>();
    words.add("Il ne lui faut que deux choses: ");
    words.add("le");
    words.add("pain");
    words.add("et");
    words.add("le");
    words.add("temps.");
    words.add("- Alfred de Vigny.");
    for(Iterator<String> iter = words.iterator(); iter.hasNext();){
      if (iter.next().length() == 4){
        iter.remove();
      }
    }
    log("Edited words: " + words.toString());

    //if used with a non-parameterized type (not recommended), 
    //then Object must be used, along with a cast
    Collection stuff = new ArrayList();
    stuff.add("blah");
    for (Object thing : stuff){
      String item = (String)thing;
      log("Thing : " + item);
    }
  }

  // PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
}

Open in new window


when i tried to run above code in eclipse getting below compilation error

Multiple markers at this line
      - Type mismatch: cannot convert from ArrayList<?> to
       List<Number>
      - Cannot instantiate the type ArrayList<?>

Please advise
//try
    List<Number> numbers = new ArrayList<Number>();
Avatar of gudii9

ASKER

import java.util.*;
import java.math.BigDecimal;

public final class ForEachExamples {

  public static void main(String... aArgs){
    List<Number> numbers = (List<Number>) new ArrayList<>();
    numbers.add(new Integer(42));
    numbers.add(new Integer(-30));
    numbers.add(new BigDecimal("654.2"));

    //typical for-each loop
    //processes each item, without changing the collection or array.
    for (Number number : numbers){
      log(number);
    }

    //use with an array
    String[] names = {"Ethan Hawke", "Julie Delpy"};
    for(String name : names){
      log("Name : " + name);
    }

    //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
    List<Number> numbers = new ArrayList<Number>();
    words.add("Il ne lui faut que deux choses: ");
    words.add("le");
    words.add("pain");
    words.add("et");
    words.add("le");
    words.add("temps.");
    words.add("- Alfred de Vigny.");
    for(Iterator<String> iter = words.iterator(); iter.hasNext();){
      if (iter.next().length() == 4){
        iter.remove();
      }
    }
    log("Edited words: " + words.toString());

    //if used with a non-parameterized type (not recommended), 
    //then Object must be used, along with a cast
    Collection stuff = new ArrayList();
    stuff.add("blah");
    for (Object thing : stuff){
      String item = (String)thing;
      log("Thing : " + item);
    }
  }

  // PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
}

Open in new window



which line. When i replaced the problematic line i am getting now more errors with words etc . please advise
Avatar of gudii9

ASKER

I tried
  List<Number> words = new ArrayList<Number>();

that is also giving errors.

import java.util.*;
import java.math.BigDecimal;

public final class ForEachExamples {

  public static void main(String... aArgs){
    List<Number> numbers = (List<Number>) new ArrayList<>();
    numbers.add(new Integer(42));
    numbers.add(new Integer(-30));
    numbers.add(new BigDecimal("654.2"));

    //typical for-each loop
    //processes each item, without changing the collection or array.
    for (Number number : numbers){
      log(number);
    }

    //use with an array
    String[] names = {"Ethan Hawke", "Julie Delpy"};
    for(String name : names){
      log("Name : " + name);
    }

    //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
    List<Number> words = new ArrayList<Number>();
    words.add("Il ne lui faut que deux choses: ");
    words.add("le");
    words.add("pain");
    words.add("et");
    words.add("le");
    words.add("temps.");
    words.add("- Alfred de Vigny.");
    for(Iterator<String> iter = words.iterator(); iter.hasNext();){
      if (iter.next().length() == 4){
        iter.remove();
      }
    }
    log("Edited words: " + words.toString());

    //if used with a non-parameterized type (not recommended), 
    //then Object must be used, along with a cast
    Collection stuff = new ArrayList();
    stuff.add("blah");
    for (Object thing : stuff){
      String item = (String)thing;
      log("Thing : " + item);
    }
  }

  // PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
}

Open in new window

please advise
How did you try it, and what errors is it giving?
Avatar of gudii9

ASKER

I tried as below. Please find my updated code.

import java.util.*;
import java.math.BigDecimal;

public final class ForEachExamples {

  public static void main(String... aArgs){
    List<Number> numbers = (List<Number>) new ArrayList<>();
    numbers.add(new Integer(42));
    numbers.add(new Integer(-30));
    numbers.add(new BigDecimal("654.2"));

    //typical for-each loop
    //processes each item, without changing the collection or array.
    for (Number number : numbers){
      log(number);
    }

    //use with an array
    String[] names = {"Ethan Hawke", "Julie Delpy"};
    for(String name : names){
      log("Name : " + name);
    }

    //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
    List<Number> words = new ArrayList<Number>();
    words.add("Il ne lui faut que deux choses: ");
    words.add("le");
    words.add("pain");
    words.add("et");
    words.add("le");
    words.add("temps.");
    words.add("- Alfred de Vigny.");
    for(Iterator<String> iter = words.iterator(); iter.hasNext();){
      if (iter.next().length() == 4){
        iter.remove();
      }
    }
    log("Edited words: " + words.toString());

    //if used with a non-parameterized type (not recommended), 
    //then Object must be used, along with a cast
    Collection stuff = new ArrayList();
    stuff.add("blah");
    for (Object thing : stuff){
      String item = (String)thing;
      log("Thing : " + item);
    }
  }

  // PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
}

Open in new window


Error is adding String values to Number list is giving compilation errors. please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The root cause of the problem is that the code that you originally used would only work on Java 7 and  above. I guess you are using Java 6 or below, hence the errors. In Java 7, the compiler allows you to omit the generic type when it can work it out from the variable that you are assigning to.

Hence, in Java 6...
List<Number> numbers = new ArrayList<Number>();

Open in new window


In Java 7, you can do this (the above is also still valid too)...
List<Number> numbers = new ArrayList<>();

Open in new window


Therefore, ozo's code is what you need for it to compile on Java 6, but note the only difference between that and your original code is the correct insertion of the generic parameter (either Number or String) inside the <> brackets
Avatar of gudii9

ASKER

Assume you have a folder that contains multiple Excel workbooks and those workbooks contain multiple worksheets and those worksheets contain multiple rows. Further assume you have a method to retrieve the folder (let's call it getDomain) and the folder has a method to retrieve the workbook

This example i can relate well because i know lot about excel workbook, sheet, cell, values as i use day to day. But when i am working with different clients like financial, banking etc when i am new to their system how to understand and remember what is getting from which getter, which object. Do i supposed to write all that on a paper which seems not effective as it sometimes takes long time and and many many methods and classes to write and confusing. please advise
Avatar of gudii9

ASKER

can you please advise on above.
Avatar of gudii9

ASKER

import java.util.*;
import java.math.BigDecimal;

public final class ForEachExamples {

  public static void main(String... aArgs){
    List<Number> numbers = (List<Number>) new ArrayList<>();
    numbers.add(new Integer(42));
    numbers.add(new Integer(-30));
    numbers.add(new BigDecimal("654.2"));

    //typical for-each loop
    //processes each item, without changing the collection or array.
    for (Number number : numbers){
      log(number);
    }

    //use with an array
    String[] names = {"Ethan Hawke", "Julie Delpy"};
    for(String name : names){
      log("Name : " + name);
    }

    //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
    Collection<String> words = (Collection<String>) new ArrayList<>();
    words.add("Il ne lui faut que deux choses: ");
    words.add("le");
    words.add("pain");
    words.add("et");
    words.add("le");
    words.add("temps.");
    words.add("- Alfred de Vigny.");
    for(Iterator<String> iter = words.iterator(); iter.hasNext();){
      if (iter.next().length() == 4){
        iter.remove();
      }
    }
    log("Edited words: " + words.toString());

    //if used with a non-parameterized type (not recommended), 
    //then Object must be used, along with a cast
    Collection stuff = new ArrayList();
    stuff.add("blah");
    for (Object thing : stuff){
      String item = (String)thing;
      log("Thing : " + item);
    }
  }

  // PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
}
 

Open in new window


My code looks as above. My eclipse seems using j2se 1.6 as attached. I tried all possible combinations like casting using ArrayList instead of List etc. Nothing seems working to me. Can you please post me complete working code for this example. I have attached different screenshots of the errors which are not clear to me. Please advise
TestPrj.jpg
TestPrj2.jpg
TestPrj3.jpg
TestPrj4.jpg
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

package eePackage;
import java.util.*;
import java.math.BigDecimal;

public final class ForEachExamples {

  public static void main(String... aArgs){
   // List<Number> numbers = (List<Number>) new ArrayList<>();
    List<Number> numbers = new ArrayList<Number>();
    numbers.add(new Integer(42));
    numbers.add(new Integer(-30));
    numbers.add(new BigDecimal("654.2"));

    //typical for-each loop
    //processes each item, without changing the collection or array.
    for (Number number : numbers){
      log(number);
    }

    //use with an array
    String[] names = {"Ethan Hawke", "Julie Delpy"};
    for(String name : names){
      log("Name : " + name);
    }

    //removal of items requires an explicit iterator,
    //so you can't use a for-each loop in this case
    Collection<String> words = new ArrayList<String>();
    words.add("Il ne lui faut que deux choses: ");
    words.add("le");
    words.add("pain");
    words.add("et");
    words.add("le");
    words.add("temps.");
    words.add("- Alfred de Vigny.");
    for(Iterator<String> iter = words.iterator(); iter.hasNext();){
      if (iter.next().length() == 4){
        iter.remove();
      }
    }
    log("Edited words: " + words.toString());

    //if used with a non-parameterized type (not recommended), 
    //then Object must be used, along with a cast
    Collection stuff = new ArrayList();
    stuff.add("blah");
    for (Object thing : stuff){
      String item = (String)thing;
      log("Thing : " + item);
    }
  }

  // PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
}
 

Open in new window

it worked as above and got below output
42
-30
654.2
Name : Ethan Hawke
Name : Julie Delpy
Edited words: [Il ne lui faut que deux choses: , le, et, le, temps., - Alfred de Vigny.]
Thing : blah
Avatar of gudii9

ASKER

I wonder why they defined log method as private as below. please advise

// PRIVATE
  private static void log(Object aThing){
    System.out.println(aThing);
  }
I wonder why they defined log method as private as below. please advise
Because that method is only called from within this class, it can be defined as "private". They could have defined it as "public" but it wouldn't need to be and so generally you only declare something to be restrictive as possible while still allowing the program to work.

But this is a totally different question, if you want to discuss further, please open a new question.
Avatar of gudii9

ASKER

I got it. Can you please advise on my above comment ID: 40412871 which is still challenge in my mind.
Avatar of gudii9

ASKER

Please advise