Chapter 1 Introduction To Ds
Chapter 1 Introduction To Ds
INTRODUCTION:
Variable: Variable is a name given to memory location
Date Type: It defines the characteristics of a Variable.
DATA TYPES IN
Primitive
(Intrinsic)
Numeric
Integer
Floatingpoint
Character
Constants
(Derived)
Non-Numeric
Charact
Classes
Boolean
Name
Byte
Short
Int
Long
Boolean
Char
Float
Double
Arrays
Interface
Size
1 byte
2 bytes
4 bytes
8 bytes
1 bit
16 bits 2 bytes
4 bytes
8 bytes
OPERATORS:
Arithmetic: +, - , * , / , %(Modules)
Logical: AND (&&) , OR (||) , NOT (!)
Increment ++, Decrement --
CLASS: class is a user defined data type that contains data, fields, and methods.
OBJECT: object is a class variable or instance of class.
class classname {
fields declaration;
method declaration;
}
Page 1 of 6
DATA STRUCTURE:
A Data Structure is an arrangement or organization of data in a computers memory
(Disk).
A Data Structure includes Arrays, Linked List, Stack, Queue, Binary Trees, Graphs and
Hash Tables.
a) Linear data structure (Arrays, Linked List, Stack, and Queue).
b) Non Linear data structure (Binary Trees, Graphs and Hash Tables).
ARRAY:
An Array is an object that consists of a sequence of numbered elements all of the same
type.
An Array Index always start with Zero.
It is a linear data structure that continues more than are value of same type. Like other
variables arrays must be declared before use :
1- Declaration.
2- Creating memory locations.
3- Initialization (Putting values in the array).
DECLARATION :
a- Type ArrayName[];
b- Type []ArrayName;
E.g. int a[];
float []b;
C REATING MEMORY LOCATIONS:
ArrayName = new type[size];
e.g. a = new int[10];
a[0]
A[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
Page 2 of 6
INITIALIZING OF ARRAY
ArrayName[index] = value;
int [] number;
number = new int[5];
10
number[0]
number[4]
number[0] = 10
}
Output:
5
1
3
2
6
5
1
3
Page 3 of 6
2
6
TWO DIMENSIONAL ARRAYS:
n[0]
[0]
int ArrayName[][];
ArrayName = new [3][4];
n = new [3][4];
n[0]
[3]
n[1]
[2]
INITIALIZATION :
0
0
2
n[3]
[0] 0
n[3]
[3]
2
3
STRING CLASS:
String: It is a collection of characters.
It is a class present in java.lang package.
String str;
str = new String("Ali");
STRING ARRAY :
String ArrayName[] = new String[4];
String Array[0] = "Ali";
String Array[3] = "Abdullah";
Ali
Abdull
ah
Make a program that will store five names of city, In a string array these names in
ascending order.
Page 4 of 6
WRAPPER CLASS: Java provides some classes that are used to convert object to data
type.
Data Types in Java
boolean
byte
char
short
int
long
float
double
Wrapper Class
Name
Boolean
Byte
Character
Short
Integer
Long
Float
Double
Size in Bits
1
8
16
16
32
64
32
64
VECTORS: is a data structure with contiguous memory like Array and is used to create
dynamic array that hold objects of any type.
Advantage of vectors:
1- Its easy to use vector to store an object.
2- Vector can be used to store a list of objects that may vary in size.
3- We can delete and add objects from list.
DISADVANTAGES OF VECTORS:
We cannot directly store simple data type in a vector.
DECLARATION OF VECTORS
Vector list = new Vector();
you should import java.util package to make the vectors working in your program.
METHODS IN VECTOR:
1- list.addElement(item)
2- list.elementAt(n)
3- list.size()
4- list.removeElementAt(n)
5- list.removeElement(item)
6- list.removeAllElements()
7- list.copyinto(Array)
8- list.insertElementAt(item,
9- list.isEmpty()
10-list.lastElement(Array)
11-list.firstElement(Array)
12-list.clear(Array)
Example:
import java.util.*
Page 5 of 6
class VectorExample {
public static void main(String args[])
{
Vector vct = new Vector;
Vct.insertElementAt("Riyadh",0);
Vct.insertElementAt("Jeddah",1);
Vct.insertElementAt("Abha",2);
Vct.insertElementAt("Makkah",3);
}
}
isEmpty()
size()
boolean
add(Object x)
boolean
contains(Object x)
boolean
remove(Object x)
void
clear()
Object[]
toArray()
Iterator
iterator()
Page 6 of 6