|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test | SCWCD mock test | JSP mock test | ORACLE mock test | OCP mock test | Hibernate mock test | Servlet mock test | Struts mock test | EJB mock test | C mock test | C++ mock test | Aptitude mock test | PMP mock test Exam |Java online test | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test
***Java Code Examples*** |Technical Talk
Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions

Download java,jsp,servlet,hibernate,spring,jdbc,jms,struts,EJB,oracle,c,c++,informatica interview questions and answers in pdf format

Download 1600 PMP Questions Free

Download 800 SCJP Questions Free

Download 600 SCWCD Questions Free

Jobs Walkin

                                                                                                                                 

Add/View Reviews , Comments
Tutorial Home
SCJP 5.0 Simulator 642+ Questions With Explanations
SCJP 6.0 Simulator 664+ Questions With Explanations
SCWCD 5.0 Simulator 556+ Questions With Explanations
SCWCD 4.0 Simulator 500+ Questions With Explanations
OCA 10g Simulator 594+ Questions With Explanations
Struts Tutorial Home
Spring Tutorial Home
Hibernate Tutorial Home
JSP Tutorial Home
JDBC Tutorial Home
Webservices Tutorial Home
EJB Fundamentals

Hibernate

  1. Advantage of Hibernate over JDBC
  2. Hibernate Setup with an web Application
  3. First Hibernate Application
  4. Hibernate mapping with Database TABLE
  5. Hibernate Data Type-Java Data Type - SQL Data Type mapping
  6. One to Many Relation in Hibernate
  7. One to Many Relation in Hibernate bi-directional
  8. Many to Many Relation in Hibernate
  9. HQL: The Hibernate Query Language
  10. Criteria Queries
  11. Criteria Queries : Equal (eq), Not Equal(ne), Less than (le), greater than (gt),greater than or equal(ge) and Ordering the results
  12. Criteria Queries: And OR conditions
  13. Hibernate generator to generate id (primary key)
  14. prevent concurrent update in Hibernate,slate object updatation in Hibernate,version checking in Hibernate

    Struts


  1. Model View Controller (MVC)
  2. Model View Controller (MVC)
  3. Struts Flow-How Struts Works?
  4. Struts Tutorial - Struts Setup- First Struts Action class setup
  5. Message Resources
  6. Validation Framework
  7. Validation Framework-client side
  8. ForwardAction
  9. IncludeAction
  10. DispatchAction
  11. LookupDispatchAction
  12. DynaActionForm
  13. DynaActionForm
  14. Struts Tutorial - Mutli-click prevention using struts tokens-Prevent Duplicate Submission
  15. Logic Iterate Map and List

JSP


  1. JSP Tutorial
  2. Introduction to JSP
  3. JSP Comments
  4. JSP Syntax
  5. JSP Scripting Elements :Scriptlet, expression, declaration
  6. JSP Directives
  7. implicit objects in JSP
  8. JSP Actions
  9. Introduction to JSP
  10. jsp:useBean
  11. The jsp:setProperty Action
  12. The jsp:getProperty Action
  13. Introduction to JSP

Spring


  1. Spring Tutorial
  2. Introduction to Spring
  3. Benefits of Using Spring Framework
  4. Inversion of Control in Spring
  5. Introduction to BeanFactory
  6. Dependency Injection in Spring
  7. Collections Setter Injection
  8. Bean Scopes in Spring
  9. Spring IOC Setup Step by Step
  10. Bean Lifecycle in Spring
  11. ApplicationContext
  12. MessageSources in Spring
  13. Web Spring MVC framework
  14. Developing Your First Spring Web Application
  15. Developing Your Second Spring Web Application with Spring Form
  16. Developing Your First Spring Web Application with Spring Validation Framework with Code Example
  17. Spring integration with Hibernate

SCJP : enhanced for loop (for-each)

SCJP 1.5/1.6 Exam Kit

Develop code that implements all forms of loops and iterators, including the use of for, the enhanced for loop (for-each), do, while, labels, break, and continue; and explain the values taken by loop counter variables during and after loop execution.

An enhanced "for" loop

The current for statement is quite powerful and can be used to iterate over arrays or collections. However, it is not optimized for collection iteration, simply because the iterator serves no other purpose than getting elements out of the collection. The new enhanced for construct lets you iterate over collections and arrays without using iterators or index variables.

The new form of the for statement has the following syntax:

for ( Type Identifier : Expression ) Statement					
					

Note that Expression MUST be an array or an instance of a new interface called java.lang.Iterable, which is meant to ease the task of enabling a type for use with the enhanced for statement. This means that the java.util.Collection now extends the Iterable interface that has the following signature:

package java.lang;

public interface Iterable<E> {
  /**     
   * Returns an iterator over the elements in this collection.     
   * There are no guarantees concerning the order in which the elements     
   * are returned (unless this collection is an instance of some class     
   * that provides such a guarantee).     
   *      
   * @return an iterator over the elements in this collection     
   */    

   Iterator<E> iterator();
}
					

As an example, consider the following method that uses the conventional for statement to iterate over a collection:

// Assume we have an instance of StringBuffer "sb"
public void oldFor(Collection c) {
	for(Iterator i = c.iterator(); i.hasNtext(); ) {
		String str = (String) i.next();
		sb.append(str);
	}
}
					

With the addition of generics, the above segment of code can be rewritten using the enhanced for statement as follows:

// Assume we have an instance of StringBuffer "sb"
public void newFor(Collection<String> c) {
	for(String str : c) {
		sb.append(str);
	}
}
					

The enhanced for statement can be used to iterate over an array. Consider the following segment of code for a method that calculates the sum of the elements in an array:

public int sumArray(int array[]) {
	int sum = 0;
	for(int i=0; i<array.length; i++) {
		sum += array[i];
	}
	return sum;
}
					

Using the enhanced for statement, this can be rewritten as:

public int sumArray(int array[]) {
	int sum = 0;
	for(int i : array) {
		sum += i;
	}
	return sum;
}
					

The for(:) loop is designed for iteration over collections and arrays.

Iterating over arrays:

// for(;;) loop
int[] ageInfo = {12, 30, 45, 55};
int sumAge = 0;
for (int i = 0; i < ageInfo.length; i++) {
	sumAge += ageInfo[i];
}	
					
// for(:) loop
int[] ageInfo = {12, 30, 45, 55};
int sumAge = 0;
for (int element : ageInfo) {
	sumAge += element;
}	
					

NOTE, that an array element of a primitive value CANNOT be modified in the for(:) loop.

Iterating over non-generic Collection:

// for(;;) loop
Collection nameList = new ArrayList();
nameList.add("Mikalai");
nameList.add("Michael");
nameList.add("Craig");
for (Iterator it = nameList.iterator(); it.hasNext(); ) {
	Object element = it.next();
	if (element instanceof String) {
		String name = (String) element;
		...		
	}
}
					
// for(:) loop
Collection nameList = new ArrayList();
nameList.add("Mikalai");
nameList.add("Michael");
nameList.add("Craig");
for (Object element : nameList) {
	if (element instanceof String) {
		String name = (String) element;
		...
	}
}					
					

Iterating over generic Collection:


// for(;;) loop
Collection<String> nameList = new ArrayList<String>();
nameList.add("Mikalai");
nameList.add("Michael");
nameList.add("Craig");
for (Iterator<String> it = nameList.iterator(); it.hasNext();) {
	String name = it.next();
	...
}

					

// for(:) loop
Collection<String> nameList = new ArrayList<String>();
nameList.add("Mikalai");
nameList.add("Michael");
nameList.add("Craig");
for (String name : nameList) {
	...
}

					

The syntax of the for(:) loop does not use an iterator for the collection.

NOTE, the for(:) loop DOES NOT allow elements to be removed from the collection.

for (Type FormalParameter : Expression) Statement
					

The FormalParameter must be declared in the for(:) loop.

The Expression is evaluated only once.

The type of Expression is java.lang.Iterable or an array. The java.util.Collection interface is retrofitted to extend the java.lang.Iterable interface which has the method prototype Iterator<T> iterator().

When Expression is an array: the type of the array element MUST be assignable to the Type of the FormalParameter.

When Expression is an instance of a raw type java.lang.Iterable: the Type of the FormalParameter MUST be Object.

When Expression is an instance of a parameterized type java.lang.Iterable<T>: the type parameter T MUST be assignable to the Type of the FormalParameter.

Restrictions on the enhanced for loop:

  • CANNOT be used to remove elements from a collection. The 'for-each' loop hides the iterator, so you CANNOT call remove(). Therefore, the 'for-each' loop is not usable for filtering:

    
    // This is NOT possible with 'for-each' loop !!!
    // Removes the 4-letter words from c
    static void expurgate(Collection<String> c) {
    	for (Iterator<String> i = c.iterator(); i.hasNext(); ) {
    		if (i.next().length() == 4) {
    			i.remove();
    		}	
    	}       
    }
    								
    								

  • CANNOT be used to modify the current slot in a collection or an array. It is not usable for loops where you need to replace elements in a list or array as you traverse it.

    
    Collection<String> nameList = new ArrayList<String>();
    nameList.add("Mikalai");
    nameList.add("Michael");
    nameList.add("Craig");
    for (String name : nameList) {
    	name = "Nicholas"; // WRONG ! Can not modify collection
    }
    System.out.println(nameList);
    								
    								
    The output:
    [Mikalai, Michael, Craig]
    								
    					
    int[] numArray = new int[] {1,2,3,4,5};
    for (int i : numArray) {
    	i = 0; // WRONG ! Can not modify array !
    }
    System.out.println(Arrays.toString(numArray));
    								
    The output:
    [1, 2, 3, 4, 5]								
    								

    However, we CAN modify object itself:

    
    Collection<StringBuffer> bufferList = new ArrayList<StringBuffer>();
    bufferList.add(new StringBuffer("Mikalai"));
    bufferList.add(new StringBuffer("Michael"));
    bufferList.add(new StringBuffer("Craig"));
    for (StringBuffer name : bufferList) {
    	name.reverse(); // CORRECT ! Can modify object itself
    }
    System.out.println(bufferList);
    								
    								
    The output:
    [ialakiM, leahciM, giarC]
    								

  • CANNOT be used to simultaneously iterate over multiple collections or arrays in parallel.

SCJP 5.0 Simulator Exam Kit
SCJP 6.0 Simulator Exam Kit
SCWCD5.0 Simulator Exam Kit
SCWCD4.0 Simulator Exam Kit
OCA 10g Simulator Exam Kit
SCJP 5.0 Simulator Free Trial
SCJP 6.0 Simulator Free Trial
SCWCD5.0 Simulator Free Trial
SCWCD4.0 Simulator Free Trial
OCA 10g Simulator Free Trial
The information you are posting should be related to java and ORACLE technology. Not political. Your Ad Here SCJP 5.0 Simulator 642+ Questions With Explanations
SCJP 6.0 Simulator 664+ Questions With Explanations
SCWCD 5.0 Simulator 556+ Questions With Explanations
SCWCD 4.0 Simulator 500+ Questions With Explanations
OCA 10g Simulator 594+ Questions With Explanations


Click to join PMP_FOURTH_EDITION

Subscribe to PMP_FOURTH_EDITION

Click to join SCJP_Mock_techFAQ360

Subscribe to techfaq360