Unit 5 Arrays Strings and Vectors (By Arabinda Saikia) Edited
Unit 5 Arrays Strings and Vectors (By Arabinda Saikia) Edited
UNIT STRUCTURE
5.2 INTRODUCTION
addition to this, the concept of the strings and vectors are also covered in
this unit in detail. In the next unit, we will explore the concepts related to
inheritance and polymorphism.
5.3 ARRAYS
You are not obliged to create the array itself when you declare
the array variable. The array variable is distinct from the array itself.
There are two ways to declare the array variables in Java:
data type arrayname[ ];
or
data type[ ] arrayname;
For example, int sum[ ];
float percentage[ ];
int roll_number[ ];
int primes[ ];
The above declaration can be written as :
int[ ] sum;
88 Programming in Java (Block 1)
Arrays, Strings and Vectors Unit 5
float[ ] percentage;
int[ ] roll_number;
int[ ] primes;
Remember that in such types of declarations no memory
has been allocated to store the array itself and the number of
elements has not been defined.
You can initialize an array with your own values when you
declare it, and at the same time determine how many elements it
will have. Following the declaration of the array variable, simply add
58.9, 59.0};
double result = 0;
int i;
for(i=0; i<10; i++)
result = result + nums[i];
System.out.println("Average is " + result / 10);
}
}
Output : Average is 54.95
Array Length : You can refer to the length of the array using length,
a data member of the array object. In our array example i.e. the
array sum, its length can be assigned to an another variable as
follows:
int size = sum.length
The following program 2 & 3 demonstrates the use of length
object for computing the largest and the smallest number from a
given list of numbers.
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
Output : Largest Number is : 83
Smallest Number is : 12
Reusing Array Variables : You can use an array variable to reference different Palindrome: A palin-
arrays at different points in your program. Suppose you have declared and drome is a sequence
of character that is
defined the variable primes as before:
identical from both
int[ ] primes = new int[10]; // Allocate an array of 10 integer elements
side i.e., from left to
This produces an array of 10 elements of type int. Perhaps you want the right and right to left.
array variable primes to refer to a larger array, with 50 elements say. You For e.g., madam , 121,
would simply write : 52125 etc.
primes = new int[50]; // Allocate an array of 50 integer elements
Now the variable primes refer to a new array of values of type int that is
entirely separate from the original. When this statement is executed, the
previous array of 10 elements is discarded, along with all the data values
you may have stored in it. The variable primes can now only be used to
reference elements of the new array.
matrix [2][1]
row 0 10 5 -3 9 2
row 1 1 0 14 5 6
row 2 -1 7 4 9 2
5.5 STRINGS
You can also test two strings for equality by using the method equals( ).
The general form of this method is s1.equals (s2) which means it will return
true if s1 equals to s2.
The following program demonstrates the equality of two string.
//Program 5.8 : To check equality of two strings.
class EqualString
{
public static void main(String args[])
{
String str1 = "BCA Second Semester";
String str2 = "BCA Third Semester";
String str3 = str1; //str1 string assigns to str3
// Finds the length of strings
System.out.println("\nLength of str1: " + str1.length());
System.out.println("\nLength of str2: " + str2.length());
System.out.println("\nLength of str3: " + str3.length());
if(str1.equals(str2))
System.out.println("str1 == str2");
else
System.out.println("str1 != str2");
if(str1.equals(str3))
System.out.println("str1 == str3");
else
System.out.println("str1 != str3");
}
}
Output :
the expression str1 == str2 will be true only if str1 and str2 are
synonyms. Here in this example, str1 and str2 are not equivalent so
it will return false and hence prints the else statement.
String Arrays : We can also create and use arrays for storing strings.
The following statement will create an string array named arraySt
of size 5 to hold 5 string constant.
String arraySt[ ] = new String[5];
We can assign the strings to the arraySt element by element
using five different statements or more efficiently using a for loop.
The following program demonstrates the use of the string
array :
//Program 5.9 : Program for string away
class arrayStr
{
static String arraySt[ ] ={"BCA 1st Sem","BCA 2nd Sem","BCA
3rd Sem","BCA 4th Sem","BCA
5th Sem", "BCA 6th Sem"};
public static void main(String args[])
{
for(int i=0;i<6;i++)
{
System.out.println(arraySt[i]);
}
}
}
Output : BCA 1st Sem
BCA 2nd Sem
BCA 3rd Sem
BCA 4th Sem
BCA 5th Sem
BCA 6th Sem
// Creation of Strings
StringBuffer strBuf1 = new StringBuffer("KKHSOU");
StringBuffer strBuf2 = new StringBuffer(40); //With capacity 40
StringBuffer strBuf3 = new StringBuffer(); //Default Capacity 16
System.out.println("\n\n strBuf1 : " + strBuf1);
// Finds the capacity of StringBuffer
System.out.println("strBuf1 capacity : " + strBuf1.capacity());
System.out.println("strBuf2 capacity : " + strBuf2.capacity());
System.out.println("strBuf3 capacity : " + strBuf3.capacity());
// Finds the length of Buffer1
System.out.println("strBuf1 length : " + strBuf1.length());
// Finds the character at position 2
System.out.println("strBuf1 charAt 2 : " + strBuf1.charAt(2));
// Appends a string to strBuf 3
strBuf3.append("Fourth Semester BCA");
System.out.println("strBuf3 when appended with a String : "
+ strBuf3.toString());
strBuf3.insert(16, 'M'); // insert ‘M’ at position 16
System.out.println("strBuf3 when M is inserted at 16 : "
+ strBuf3.toString());
strBuf3.delete(11, 's'); // delete the substring from 11 position
System.out.println("strBuf3 when s is deleted at 11 : "
+ strBuf3.toString());
strBuf3.reverse(); // reverse the string constant of strBuf3
System.out.println("Reversed strBuf3 : " + strBuf 3);
strBuf2.setLength(12); //set the length of strBuf 2 to 12
strBuf2.append("java programs"); // appends the string to strBuf2
System.out.println("strBuf2 : " + strBuf2);
}
}
Output :
or
Vector V = new Vector(3); // declaring with size
The vector class supports a number of methods that can be used to
manipulate the vectors created. Some are listed in table 5.1:
Wrapper Classes : Vectors can not handle primitive data types like int,
float, long, char and double. Primitive data types may be converted into
object types by using the wrapper classes contained in the java.lang
package. Table 5.2 shows the simple data types and their corresponding
wrapper class types.
*****