0% found this document useful (0 votes)
31 views

Control Statements, Collection, Generics

This document contains examples of control flow statements like if-else, while, do-while, and for loops in Java. It also provides examples of using collections and generics. The examples demonstrate basic usage of control flow statements to iterate through loops and print values. Collections and generics are shown to organize data and allow for type-safe containers and methods through parameterized types.

Uploaded by

Anjana Ranjan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Control Statements, Collection, Generics

This document contains examples of control flow statements like if-else, while, do-while, and for loops in Java. It also provides examples of using collections and generics. The examples demonstrate basic usage of control flow statements to iterate through loops and print values. Collections and generics are shown to organize data and allow for type-safe containers and methods through parameterized types.

Uploaded by

Anjana Ranjan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java • Control flow statements

{ •


Collections
Generics
Example 1: if-else
/*
A simple example class with a basic main method
that instantiates several objects of the class,
manipulates the objects, and outputs information
about the object
*/
public class SimpleExample {
private int number;
public SimpleExample() { }
public void setValue(int val) {
number = val;
}
public int getNumber() {
return number;
}
public static void main(String[] args) {
for(int i=0;i<10;i++) {
SimpleExample example = new SimpleExample();
if(i/2 <= 2) {
example.setValue(i);
} else {
example.setValue(i*10);
}
System.out.println("SimpleExample #"+i+
"'s value is "+example.getNumber());
}
}
}
Example 2: while loop
//-- the while loop
int i = 0;
// until i is equal or greater than 10 does the
loop continue
while(i < 10) {
System.out.println(String.valueOf(i));
i++;
}
Example 2: do-while loop
//-- the do while loop
int k = 0;
// until k is equal or greater than 10 does
the loop continue
do {
System.out.println(String.valueOf(k));
k++;
} while(k < 10);
Example 2: for loop
//-- the for loop
// the loop initializes j to 0, then increments
it by +1
// (the j++) until j is equal to or greater than
10
for(int j=0;j<10;j++) {
System.out.println(String.valueOf(j));
}
Example 2: for-each loop
//-- for each loop
String[] arr =
{"The","Quick","Brown","Fox"};
// the loop iterates through the entire array
for(String a: arr) {
System.out.println(a);
}
Collection: Example
// Java program to show whey collection framework was needed
class Test
{
public static void main (String[] args)
{
// Creating instances of array, vector and hashtable
int arr[] = new int[] {1, 2, 3, 4};
Vector<Integer> v = new Vector();
Hashtable<Integer, String> h = new Hashtable();
v.addElement(1);
v.addElement(2);
h.put(1,"geeks");
h.put(2,"4geeks");
}
Method Description

boolean add(Collection) Appends the specified element to the end of a list.

void add(int index, Object


Inserts the specified element at the specified position.
element)

void clear() Removes all the elements from this list.

Return the index in this list of the last occurrence of the specified element, or -
int lastIndexOf(Object o)
1 if the list does not contain this element.

Object clone() Return a shallow copy of an ArrayList.

Object[] toArray() Returns an array containing all the elements in the list.

void trimToSize() Trims the capacity of this ArrayList instance to be the list’s current size.
Generics: Syntax

// To create an instance of
generic class BaseType
<Type> obj = new BaseType
<Type>()
Generics: Example
public static void main (String[] args)
{
// instance of Integer type
Test <Integer> iObj = new Test<Integer>(15);
System.out.println(iObj.getObject());

// instance of String type


Test <String> sObj =
new Test<String>("Generics");
System.out.println(sObj.getObject());
}
OUTPUT:
15
Generics

You might also like