Array, String & Vectors

Home   »    Java   »    Array, String & Vectors


Arrays refer to a named list of final numbe 'n' of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0,1,2,3.....n. If the name of an array of 10 elements is ARY, then its elements will be referenced as shown below:

ARY[0] , ARY[1] , ARY[2] , .................ARY[10] ;

Arrays can be one dimensional, two dimensional or multi dimensional.

Strings Manipulation is the most common part of the many Java programs. Strings represent a sequence of characters. Strings are class objects in Java and using two classes namely, String and String puffa String are more predictable and reliable in Java, because java compiler do the bound checking for strings.

Contents



Arrays

When there is a need to use many variables then we There is a big problem because we will Conflict with name of variables So that in this Situation where we wants to Operate on many numbers then we can use array .Arrays are Set of Elements having Same data type or We can Say that Arrays are Collection of Elements having same name and same data type But Always Remember Arrays are Always Start From its index value and the index of array is start From 0 to n-1. Suppose we wants to Access 5th Element of array then we will use 4th Element Because Arrays are Start From 0 and arrays are always stored in Continuous Memory Locations The Number of Elements and Types of array are Identified by Subscript of array Elements. The Various types of Array those are provided by java as Follows:-





One-Dimensional Arrays

These are Mostly used when we wants to declare number of Elements Single Dimensional arrays are used for Using Many elements and they are Declared with Single Bracket Like int a [] etc. the Bracket will specify the size of array Elements Means how many array elements are declared and used For Accessing or using a variable we have to Specify the Index Value in the Sub Script or in Bracket of Array Variable The [] is used for dimensional or the sub-script of the array that is generally used for declaring the elements of the array For Accessing the Element from the array we can use the Subscript of the Array like this

a[3]=100;

So there is only the single bracket then it is called as Single Dimensional Array

The Syntax For Declaring Array Elements in java is as Follows-

int a[]=new int[5];

Note the new Keyword this is a Special Operator Which is usef for Creating new Memory which is Continuous in nature and The new Keyword is Also Creating For Memory For Number of elements That is Specified in Single bracket.

The Complete One-Dimensional Array Program below:





Download Complete Program

  -  



Two-Dimensional Arrays

The Two Dimensional array Elements are used when we Wants to Perform the Operation in Matrix Forms the Two Dimensional arrays are used For Creating Matrix and Displaying array elements in the Form of Rows and Columns The Total Elements of Arrays Will be Calculated by Multiplying the Elements of Rows and Columns Like int a[3][3] i. e 9 Elements will be used by Array These are Called as Two Dimensional Because they use two Brackets.

Like this int a[3][3];

So This is the Example of the Two Dimensional Array In this first 3 represents the total number of Rows and the Second Elements Represents the Total number of Columns The Total Number of elements are judge by Multiplying the Numbers of Rows * Number of Columns in The Array in the above array the Total Number of elements are 9. The Two Dimensional arrays are Declared as Follows:-

Int a[][]=new int[3][3];

The Complete Two-Dimensional Array Program below:




Download Complete Program

  -  







Strings

Like an integer characters are also be in the Array The Array of Characters are called as the Strings They are Generally used for representing the Strings in java String is Also Array and Also a Class .we Know that when Collection of Characters are called as String In java For Creating an String array You Will use as Follows:-

String s[]=new String[10];

Here is the s is an array of type String Means it can accept only 10 Characters but if you will use String as then:-

String s = new String("Welcome To Strings");

S is an object of Class String and now you can use any Method From String Class The Various String Methods those are Reside in String Class are :-

A. ToLowerCase:- For Converting a String into small letters or in small cases.
B. ToUpperCase:- For Converting a String into Upper letters or in Upper cases.
C. Length:- For Calculating the Number of Characters in a String.
D. Trim:- For Removing both left and Right Side Spaces.
E. charAt:- For Finding a Character at the Position.
F. indexOf:- For Finding the Position of a Character in a String.
G. substring:- For Extracting a string from a string.
H. replace:- For Replacing a Character in the String.
I. compareTo:- For Comparing a String With another.
J. Concat:- For Joining Two strings and Making a Single String.


String and String Method:



Output of "Str_StrMethod.java"


Download Complete Program

  -  



StringBuffer Class
We Know that String Class provides us various Methods for Operating on a Single String But A String that is Created with the help of String class will be Static means it doesn't provide any Methods for Extending the Length of String For Example if you Wants to Insert a Character or a String into Existing String the it is not Possible So that String that is Created by String Class is static For This Purpose We uses String Buffer Class Which Provides us the Facility to Extend the Operation on Length of a String Like Removing Characters From String or either Inserting String into Existing String The Various Methods those are Provided by String Buffer Class are as follows:-

1. setChatAt(n,'c'):-For Inserting a Char c at nth Position

2. append(string):- For Inserting a String in last at Existing String

3. insert(n,s):- inserting a String at nth Position

4. setLength:- Specifying the Length of a String



Output of "Str_Buffer.java"


Download Complete Program

  -  



Vectors

We know that Arrays are very useful when there is a need to use number of variables But There is a problem with Array they use only single data type or The Elements of array are always Same type For Avoiding this Problem Vectors are used These are also Collection of elements those are object data type For Using Vectors we have to import java.util package These are also Called as dynamic Array of object data type But Always Remember vectors doesn't support primitives data types like int, float, char etc. For Creating a Vector, Vector Class will be used which is reside in java's utility package Various Methods those are used with vectors:-

1. addElement:- For Inserting an element into Vector.
2. elementAt:- Gives the name of Char at position.
3. size:- Give us Size of Number of Elements in Vector.
4. copyInto:- For Copying All Elemnts insto an Array.
5. insertElementAt(ele,n):- For Inserting an Element ele into the nth Position.

Vectors:


Output of "VectorClass.java"


Download Complete Program

  -  




Wrapper Classes

We know that Vectors can't handle Primitives data types like int, float ,char, long So that Primitives data type may be Converted into object data types by using wrapper classes those are contained in java.lang packages The Various Wrapper Classes are :-
1) Integer
2) Float
3) Character

These are used When a List Contains Elements those are String in nature and we wants to convert them into primitives data type then we have to use Wrapper Class Wrapper Classes will First Convert the String into the object data type eithet it may beint, float ,char etc and after that we will be able to convert the object data type into primitives date type Many Times we were using Wrapper Class Like Integer.parseInt("123") This is also a Wrapper Class (Integer) For Converting a String into int data type.





<< Previous Topic
Next Topic >>



6 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  2. Very nice vector tutorial. you have explained it very well . There is also good java vector tutorial Click Here

    ReplyDelete
  3. Thanks for this valuable blog. It was very informative and interesting. Keep sharing this kind of stuff.
    Matlab Training in Chennai
    HTML5 Training in Chennai
    Matlab Course in Chennai
    HTML5 Courses in Chennai

    ReplyDelete
  4. virtual edge High Play - everything takes place in a playful, albeit respectful environment. mothers day quotes, fish puns and skeleton puns

    ReplyDelete