Overview
- Types of variables in Java
- About Arrays
- When to use Arrays ?
- Conventional approach to solve a problem?
- Conclusion
- Array data structure
Types of Variables In Java
- Value type variables ( ordinary variables of primitive types)
- Nullable type variables (Reference variables of non primitive type
or array reference variables of any type.
- int x;
float y;
Student s1;
int[]a;
Student[]s;
About Arrays
- Array is a linear collection of similar
elements. - Array elements are indexed.
- Array in java is always created dynamically.
- [ ] is called subscript operator.
- All arrays have length property which
contains the length of the array. - int[]a=new int[5];
a->length[5] - Accessing Array elements is fast
- It takes constant time to access any
item of the array it index is
known.
When To Use Arrays?
- whenever group of related data is need to be stored.
- whey data is in a group of groups
To solve a programming problem, you have to store marks of 100 students .How can you implement it?
- int [] a = new int[100];
Suppose you have created an array as :
- int []a = new int[100];
And assume that you have stored some out of 100 data in this array .
Now answer following questions
- How many elements are stored in the array ?
- IF suppose 10 elements are stored and then I want to store one more element at index 2 . Can we do it as a[2]=data;
- How to guard against overflow or underflow?
- How to delete an element ?
Conclusion
- Normal array is not good enough to
efficiently handle such situation - we need to keep extra information
like ,index of last filled block
( assume elements are filled from left
to right) to track the number of elements
present in the array. - We need to create an array data structure in Java.
- Define a class Array with appropriate number of variables and functions .