Professional Documents
Culture Documents
Loops and Data Structures
Loops and Data Structures
Loops
Definition
A loop is a way of repeating lines of code more than once. The block of code contained within the loop will be executed again and again until the condition required by the loop is met. In Java There are 3 loops statements
Do..While Loop
Types of Loops
For Loop Example Public static void main(String[] args) { for (int i =0; i< 12 ; i++) { System.out.println(Java); } }
The Condition While this condition is true the loop will contains
{
System.out.println("We're looping around.."); if (numbers[index] == 10) { numberFound = true; System.out.println("We've found the number " );` } index++;
Do..While Example
Public static void main(String [] args) { int x = -1 ; do { System.out.println(x); } while(x>0); }
Excercises
1.
Make an application that takes an input string and reverse it Make an application that calculates how many number an input string repeated.
Sample input I can say hello to say yes
2.
Data Structures
if you want to store the daily temperature values for the whole year. Will you make a 356 variable to store them ?!
if you want to store student marks and you dont know how many subject he has ? , You will ask him at first then ?
What
Array
is a data structure that stores fixed number of data elements of the same type. Array elements are indexed in a zero-based fashion. ( first element index is zero).
Declaration
Initialization
Identifier
Array Size
Initialization
Identifier
Array Elements
Length
Arrays have an instance variable length, which gives the capacity of the array.
int x [] = new int [10]; x.length; Note that length is an instance variable associated with arrays, while length() is a method associated with strings.
temperatureValues[0] = 30; // initialize first element temperatureValues[1] = 31; // second element temperatureValues[2] = 32.5; // etc. temperatureValues[3] = 30; temperatureValues[4] = 29; temperatureValues[5] = 18; temperatureValues[6] = 18;
Usually loops is the way you fill and retrieve array elements.
{
// code to enter the value array[i] = insertedValue; }
Exercises
1.
2.
3.
4.
Collections
Collections
A collection is an object that can be used for storing, retrieving and manipulating groups of items. To use Collections we must import java.util.*
They use the wrapper class of any datatype
For more about collections visit this link
http://www.tutorialspoint.com/java/java_collections.htm
Wrapper Classes
1) ArrayList
Objects of the generic class ArrayList are similar to arrays, but have differences:
Similarities
Both can store a number of references the data can be accessed via an index.
Differences
The ArrayList object requests more space from the Java run-time system, if necessary.
The ArrayList is not ideal in its use of memory.
As soon as an ArrayList requires more space than its current capacity, the system will allocate more memory space for it. The extension is more than required for the items about to be added. So there will often be empty locations within an ArrayList that is, the size will often be less than the capacity.
The capacity of an ArrayList object is the maximum number of items it can currently hold (without being extended). The size of an ArrayList object is the current number of items stored in the ArrayList.
Declaration
How to declare an ArrayList?
Ex1: ArrayList<String> list1 = new ArrayList<String>(); This declaration of an ArrayList specifies that it will hold String references The type String can be replaced by any object type.
This declaration sets up an empty ArrayList The initial capacity is set to a default value. The ArrayList capacity is expanded automatically as needed when items are added.
Ex2:
This declaration of an ArrayList specifies that it will hold Date references The type Date can be replaced by any object type. This declaration can be used to specify the initial capacity in this case 100 elements. This can be more efficient in avoiding repeated work by the system in extending the list, if you know approximately the required initial capacity.
Arraylist Example
Conversion
ArrayList To Array
Array
To ArrayList
new ArrayList(Arrays.asList(myArray));
Set
The same as the ArrayList in every thing same concept and same methods Difference
1. 2.
TreeSet Example
Set<Integer> numbers = new TreeSet<Integer>();
numbers.add(5);
numbers.add(2);
System.out.println(numbers); // "[2, 5] System.out.println(numbers.contains(7)); // "false System.out.println(numbers.add(5)); // "false" System.out.println(numbers.size()); // "2"
HashSet Example
Set<Integer> numbers = new HashSet<Integer>();
numbers.add(5);
numbers.add(2);
System.out.println(numbers); // "[5, 2]
System.out.println(numbers.contains(7)); // "false
System.out.println(numbers.add(5)); // "false"
System.out.println(numbers.size()); // "2"
Maps
In many applications we require two types of object to be associated with each other
A hash table can store pairs of linked items in keyvalue pairs. In Java, the programmer must specify the type of both the key and the value; for example, <String, Integer>
How to declare a HashMap? Similar to ArrayList but with the types of both the key and value.
HashMap<String,String> table1 = new HashMap<String,String>();
http://www.tutorialspoint.com/java/java_treemap_class.htm
Map Methods
Map Example
Examples
1. Calculating
2. Address
book example
Questions ?
END