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

Expt No.06

The document discusses arrays and vectors in Java. It explains that arrays store fixed numbers of elements of the same type, while vectors are dynamic arrays that allow elements of any object type. The document also provides examples of declaring, initializing, and traversing single and multi-dimensional arrays as well as creating and using vector objects in Java code.

Uploaded by

Arju Salmani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Expt No.06

The document discusses arrays and vectors in Java. It explains that arrays store fixed numbers of elements of the same type, while vectors are dynamic arrays that allow elements of any object type. The document also provides examples of declaring, initializing, and traversing single and multi-dimensional arrays as well as creating and using vector objects in Java code.

Uploaded by

Arju Salmani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PARSHVANATH CHARITABLE TRUST'S

A.P. SHAH INSTITUTE OF TECHNOLOGY


Department of Computer Science and Engineering
Data Science

Subject: SBL-OOPM Class: SE-DS


Semester: III A.Y. 2022-2023
Experiment No. 6

❖ Aim : To write a Java program on 2D array, and also demonstrate the use of String Buffer and
Vector.
❖ Objectives : To study the differance between array & vector in java alongwith its use in java
program.
❖ Prerequisites : Students should know about array concept in C along witharray declaration,
initialization & its advantages.
❖ Software used : jdk 1.6.0
❖ Theory :
1] Array in Java :
• array is a collection of similar type of elements that have contiguous memory location.
• Java array is an object that contains elements of similar data type.
• It is a data structure where we store similar elements.
• We can store only fixed set of elements in a java array.
• Array in java is index based, first element of the array is stored at 0 index.

➢ Advantage of Java Array


• Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
• Random access: We can get any data located at any index position.
➢ Types of Array in java
There are two types of array.
• Single Dimensional Array
• Multidimensional Array
➢ Single Dimensional Array in java
➔ Syntax :
dataType[] arr;
(or) dataType[] arr;
(or) dataType arr[];

➢ Instantiation of an Array in java


ArrayRefVar=new datatype[size];

➢ Example of single dimensional java array


➔ example of java array, where we are going to declare, instantiate, initialize and traverse
PARSHVANATH CHARITABLE TRUST'S

A.P. SHAH INSTITUTE OF TECHNOLOGY


Department of Computer Science and Engineering
Data Science

an array.

➔ OUTPUT :
10
20
70
40
50

➔ Declaration, Instantiation and Initialization of Java Array


We can declare, instantiate and initialize the java array together by:

➔ Passing Array to method in java


We can pass the java array to method so that we can reuse the same logic on any array.
➔ Example :
Example to find minimum number of an array by passing array to method.
PARSHVANATH CHARITABLE TRUST'S

A.P. SHAH INSTITUTE OF TECHNOLOGY


Department of Computer Science and Engineering
Data Science

OUTPUT : 3

➢ Multidimensional array in java :


➔ In such case, data is stored in row and column based index (also known as matrix form).
➔ Syntax to Declare Multidimensional Array in java

➔ instantiate Multidimensional Array in java


Int[][] arr=new int[3][3]; //3 row and 3 column
➔ initializing Multidimensional Array in java
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
PARSHVANATH CHARITABLE TRUST'S

A.P. SHAH INSTITUTE OF TECHNOLOGY


Department of Computer Science and Engineering
Data Science

➢ Disadvantage of Java Array


• Size Limit: We can store only fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in java.

2] Vector In Java :
– We know that Arrays are very useful when there is a need to use fix number ofvariables
– There is a problem with Array i.e. they use only single data type or The Elements of
array are always Same type For Avoiding this Problem Vectors are used.
– Vectors are Collection of elements those are object data type.
– For Using Vectors we have to import java . util package . These are also Calledas
dynamic Array of object data type .
– For Creating a Vector, Vector Class will be used which is reside in java’sutility
package
– vectors doesn’t support primitives data types like int, float, char etc.
– Three ways to create vector class object:

1. Method 1:

It creates an empty Vector with the default initial capacity of 10. It means the Vector
will be re-sized when the 11th elements needs to be inserted into the Vector.
Note: By default vector doubles its size. i.e. In this case the Vector size would remain
10 till 10 insertions and once we try to insert the 11th element It would become 20
(double of default capacity 10).
2. Method 2:

It will create a Vector of initial capacity of mentioned value.

3. Method 3:

e.g.

Here we have provided two arguments. The initial capacity is 4 and


capacityIncrement is 6.

CONCLUSION : Summarise what you understood from this lab.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy