by Dinesh Thakur

Before using the element of an array, it must have an initial value. In some programming languages ​​do not set initial values default, and then when trying to access to an item an error occurs. In Java, all the variables, including the elements array have initial default value (default initial value). This default initial value is equal to 0 in numeric types or equivalent types (eg null for objects and false for boolean).



 
by Dinesh Thakur

In Java arrays have a fixed length that is specified during initialization, and determines the number of its elements. Once we set the length of the array is not possible to change it.



 
by Dinesh Thakur

The Vector object store objects of any kind are extremely  although flexible operations on objects of this class are slower than  operations on common vectors (arrays). The capacity of the vector is the maximum amount of  elements that it can hold at a given instant. Its size corresponds to  number of elements actually stored. Having your whole capacity occupied  (size = capacity), adding a new element causes the automatic increase  capacity vector. If an increment is specified via its constructor, this increase occurs with this granularity, otherwise the capacity is doubled.

 
by Dinesh Thakur
 
by Dinesh Thakur

When we want to enter details of several students or workers or patients, then instead of making separate objects by different names, it is better to make an array of object of that class. By making an array of object, we can take the advantage of loops to feed and retrieve data.

 
 
by Dinesh Thakur

Insertion and deletion at the beginning of a linked list are very fast. They involve changing only one or two references, which takes 0(1) time. Finding, deleting, or insertion next to a specific item requires searching through, on the average, half the items in the list. This requires O(n) comparisons. An array is also O(n) for these operations, but the linked list is nevertheless faster because nothing needs to be moved when an item is inserted or deleted.

 
by Dinesh Thakur

A linked list refers to a collection of nodes connected to each other with the help of a link. Each link object contains a reference usually called next to the next link in the list. Although an array is a common way of storing data elements, but inserting an element in between an array or deleting an element from an array requires a lot of shuffling of other array elements.

 
by Dinesh Thakur

The elements() method of the Vector class is used to retrieve an enumeration of the elements that were added to the vector. A while loop is then used to cycle through and print the elements contained in the enumeration. The hasMoreElements() method is used to determine whether the enumeration contains more elements. If it does, the nextElement() method is used to retrieve the object for printing.

 
by Dinesh Thakur

The Vector class provides the capability to implement a growable array. The array grows larger as more elements are added to it. The array may also be reduced in size, after some of its elements have been deleted. This is accomplished using the trimToSize() method.

 
by Dinesh Thakur

StringBuffer represents grow able and write able character sequences. We can modify the StringBuffer object, i.e. we can append more characters or may insert a substring in middle. StringBuffer will automatically grow to make room for such additions and is very flexible to the modifications.

 
by Dinesh Thakur

The String class includes several methods that compare strings or substrings within strings. To perform a comparison that ignores case differences, use equalsIgnoreCase(). This method considers lowercase and upper case characters as same while comparing strings. That is, it considers A-Z to be the same as a-z.

 
by Dinesh Thakur

Java does not allow operators to be applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. We can also concatenate strings with other types of data after converting them into strings. Java converts other data types into its string representation by calling valueOf() defined by String. For the simple types, valueOf() returns a string equivalent of the value with which it is called. For objects, valueOf()calls the toString() method on the object.



 
by Dinesh Thakur

Matrix addition means addition of respective positions. That is, element of 0th row 0th column of first matrix will be added with 0th row and 0th column of second matrix and placed in resultant matrix at 0th row and 0th column position. And this way all the element positions of first matrix are added with the respective positions of second matrix and stored in resultant matrix at the same position.

 
 
 
by Dinesh Thakur

Elements that can be added to the Vector must be of type java .lang. Object. In other words, vectors cannot handle primitive data typeslike int, float, char and double as they are not objects. So in order to work with primitive typevariables as if they were objects, Java provides class for each of the primitive types. These classesare known as wrapper classes.



 
by Dinesh Thakur

Arrays have fixed length, they are not suitable for group of things that grow and shrink over the lifetime of an application. In such situations, Java provides the Vector class which is available in the standard java.util package.



 
by Dinesh Thakur

StringBuffer deleteCharAt (int index): This method deletes the character at the specified position index from the string buffer. For example,

 
by Dinesh Thakur

StringBuffer delete (int start, int end): This method is used to remove a sequence of characters from the StringBuffer object. The start represents the index of the first character to be deleted and end represents the index of one past the last character to remove. Therefore, the deleted substring runs from start to end -1. For example,

 
by Dinesh Thakur

StringBuffer insert (int index, char [] str, int offset, int len) :This method inserts a substring into the StringBuffer object starting at position index. The substring is the string representation of len characters from the str []array starting at index position offset.

 
by Dinesh Thakur

StringBuffer append(char[] str,int offset,int len) :This method appends the substring to the end of the string buffer. The substring appended is a character sequence beginning at the index offset with the number of characters appended equal to len of the character array str. For example,

 
by Dinesh Thakur

String toString () : This method returns the value of the invoking StringBuffer object that invoked the method as a string. For example: The statement,

 
by Dinesh Thakur

void setCharAt (int index, char ch) : This method sets the character at the specified index in the string buffer to the new character ch. For example, s1. setCharAt (6, 'J') will return Hello Java.



 
by Dinesh Thakur

String substring (int beginIndex) : This method returns a substring of the invoking StringBuffer. The substring returned contains a copy of characters beginning from the specified index position beginIndex and extends to the end of the string. For example, s1. substring (3) returns the substring lo Java.



 
by Dinesh Thakur

char charAt (int index) : This method returns the character in the string buffer at the specified index position. The index of the first character is 0, the second character is 1 and that of the last character is one less than the string buffer length. The index argument specified must be greater than or equal to 0 and less than the length of the string buffer. For example, strl.charAt(1) will return character e i.e. character at the index position 1 on the StringBuffer str1.

 
by Dinesh Thakur

void setLength(int newLength) : This method sets (change) the length of the string contained in the StringBuffer object. If the newly specified length is less than the current length of the string buffer, then the string buffer is truncated to contain exactly the number of characters contained in the newlength parameter. If the specified argument is greater than or equal to the current length, sufficient null characters (‘\ u0000') are appended to the string buffer until the total number of characters in the string buffer is equal to the specified length. For example, if following is part of a statement, strl.setLength(10) As a result of this, the string referenced by strl will now contain "Welcome\u0000\u0000\u0000" Similarly, strl. setLength (5) will result in “Hell" .

 
by Dinesh Thakur

int length (): This method returns the number of characters currently in the invoking string buffer. For example,

 
by Dinesh Thakur

int capacity () : This method returns the current capacity of the string buffer i.e. number of characters it is able to store without allocating more memory. While discussing capacity () methods, we assume that the StringBuffer object str1 exists which is defined as follows,



 
by Dinesh Thakur

The String class also provide methods for extracting characters from a string in an array of bytes. This is achieved using the getBytes () method in the String class. This method converts the original string characters into the character encoding used by the underlying operating system which is usually ASCII . For example : The statement,



 
by Dinesh Thakur

As we know that strings are not arrays so the String class also provides methods to convert a string to an array of characters and vice versa. These include

 
by Dinesh Thakur

Programmers often need to retrieve an individual character or a group of characters (substring) from a string. For example: In a word processing program, when a part of string is copied or deleted. While charAt () method returns only a single character from a string, the substring () method in the String class can be used to obtain a substring from a string.

 
by Dinesh Thakur

boolean matches (String regex): This method tests whether or not the invoking string matches the given regular expression regex. It returns true if invoking string matches the given regular expression otherwise it returns false.It is different from the equals () method where you can only use the fixed strings. Forexample,

 
by Dinesh Thakur

String [] split (String regex):This method splits the invoking string into substrings at each match for a specified regular expression regex and returns a reference to an array of substrings. For example, consider the statements.

 
by Dinesh Thakur

String replaceFirst (String regex, String replace): This method replaces the first substring in the invoking string that matches the given regular expression with the specified replacement string replace. For example,

 
by Dinesh Thakur

String replaceAll (String regeX, String replace): This method replaces all substrings in the invoking string that matches the given regular expression pattern regeX with the specified replacement string replace. For example,

 
by Dinesh Thakur

String replace (char oldchar, char newchar): This method returns a new string resulting from replacing all the occurrence of oldchar in the invoking string with newchar. For example,

 
by Dinesh Thakur

String trim (): This method returns a copy of the string, with leading and trailing white spaces omitted. For example,

 
by Dinesh Thakur

String toLowercase (): This method converts all of the characters in the string to lowercase.

For example,

 
by Dinesh Thakur

String toUppercase ():This method converts all of the characters in the invoking string to uppercase. For example,

 
by Dinesh Thakur

The String class also provides a number of methods for comparing strings. While discussing these, we assume string sl contains 'Welcome' in it.

 
by Dinesh Thakur

Searching a string for finding the location of either a character or group of characters (substring) is a common task. For example: In some situations, usernames and passwords are stored in a single string in which they are separated from each other by a special character such as colon (Example: username : password). In order to get the username and password separately from such a string, there must be some method such that we can search the string and return the location of the colon character. The String class provides the following methods for performing these tasks,

 
by Dinesh Thakur

The String class provides methods for obtaining length, retrieving individual characters and concatenating strings. These include

 
by Dinesh Thakur

The String object can be created explicitly by using the new keyword and a constructor in the same way as you have created objects previously. For example: The statement

 
by Dinesh Thakur

Just like one-dimensional arrays, a two-dimensional array can also be passed to a method and it can also be returned from the method. The syntax is similar to one-dimensional arrays with an exception that an additional pair of square brackets is used.

 
by Dinesh Thakur

when we created arrays of arrays (i.e. two-dimensional array), the arrays in the array were of same length i.e. each row had the same number of elements. As each array in a multidimensional array is a separate array so it can have different number of elements. Such arrays are known as non-rectangular arrays or ragged arrays.

 
by Dinesh Thakur

Like one dimensional array, one can also initialize multidimensional array when they are declared. For example: A two-dimensional array table with 2 rows and 3 columns can be declared and initialized as follows,

 
by Dinesh Thakur

One-dimensional array that uses a single index. Just like other languages like C/C++ one may think that Java might also have multidimensional arrays too, which are arrays that contain more than one index and can store information in multiple dimensions. But Java does not truly support multidimensional arrays. However, one can achieve the same functionality by declaring an arrays of arrays.



 
by Dinesh Thakur

Just like a method can return a primitive type, similarly a method can also return an array. If a method returns an array then its return type is an array. This is really useful when a method computes a sequence of values.

 
by Dinesh Thakur

Just as you can pass primitive type values to methods, you can also pass arrays to a method. To pass an array to a method, specify the name of the array without any square brackets within the method call. Unlike C/C++, in Java every array object knows its own length using its length field, therefore while passing array's object reference into a method, we do not need to pass the array length as an additional argument. For example: Suppose we have an integer array num containing 10 elements.



 
by Dinesh Thakur

With the release of version 1.5, Java introduced a new type of for loop known as enhanced or numerical for loop. It is designed to simplify loops that process elements of array or collection. It is useful when you want to access all the elements of an array but if you want to process only part of the array or modify the values in the array then use the simple for loop.



 
by Dinesh Thakur

Just like simple variables can store different values at different times, an array variable can also be used to store a reference to different arrays at different times. Suppose you have created and initialized array variable num using the following statement,

 
by Dinesh Thakur

After you create an array, you can access its length i.e. number of elements it holds. You can refer to the length of an array using its length field (which is implicitly public and final).A length field is associated with each array that has been instantiated. Because length field is a public member so it can be directly accessed using the array name and a dot operator. Its syntax is

 
by Dinesh Thakur

When an array is created, each element of the array is set to default initial value according to its type. However, it is also possible to provide value other than the default values to each element of the array.

 
by Dinesh Thakur

Once the array is created, you can access an array element by using the name of the array followed by an index enclosed between a pair of square brackets. The index or subscript of an array indicates the position of an element in the array. The index of the first element in the array is always 0 (zero), the second element has an index 1 and so on. The index of the last element is always one less than the number of elements in the array. The syntax for accessing an array element is

 
 
by Dinesh Thakur

Integer.parseInt() method is used for converting the data fed in form of strings into integer form.

 
by Dinesh Thakur

Array.sort():- It is a predefined method of Array class present in java.util package. It used to sorts all the elements of an array in ascending order.



 
by Dinesh Thakur

StringBuffer reverse () : This method reverse the character sequence contained in the string buffer. In other words, the first character becomes the last, the second character becomes the second last and so on. For example, The Word “welcome” after reverse.

 
by Dinesh Thakur

Algorithm For ReverseIT:

 

step 1: set s="Dat saw 1was Tad"

step 2: set len=s.length()

step 3: create two character array c[], cl[] of size len

step 4: initialize i=0

step 5: repeat through step-7 while (i < len)

step 6: c[i]=s. charAt(i)

 
by Dinesh Thakur

charAt():- This is a predefined method of String class present in java.1ang package. It used to extract a specify character from a string by the particular index supplied by the programmer and the index must be within the length of the String.



 
by Dinesh Thakur

Algorithm for String Sorting:

step 1: Set two String variables sl,s2, four integer variables count1, count2, size 1, size2 as static

step 2: Read sl,s2

step 3: Set String variable s3=sl.toLowerCase()

step 4: Set String variable s4=s2.toLowerCase()

 
by Dinesh Thakur

Algorithm for CharacterInString:

step 1: create an integer array a[] of size 26

step 2: initialize i=0

step 3: repeat through step-5 while (i < 26)

step 4: a[i]= 0

 
by Dinesh Thakur

toCharArray():-This method is a predefined method of String present in lang package.It used to convert a String into array of character.

 
by Dinesh Thakur

Algorithm for Transpose of a Matrix:

step 1: read r, c

step 2: create an integer array a[][] of size r,c

step 3: create an integer array b[][] of size c,r

step 4: initialize i=0

step 5: repeat through step-10 while (i < r)

 
by Dinesh Thakur

Algorithm for Swap Elements of an Array:

 

step 1: read y

step 2: create two integer arrays a[],b[] of size y

step 3: initialize x=0

step 4: repeat through step-6 while (x < a.length)

step 5; read b[x]

 
by Dinesh Thakur

Algorithm for Linear Search:

step 1: read arr_size

step 2: create an integer array a[] of size arr_size

step 3: initialize i=0

step 4: repeat through step-6 while (i < arr_size)

step 5: read a[i]

 
by Dinesh Thakur

Algorithm for Insertion Sort:

Here 'a' is an array of elements and 'n' represents the number of elements in the array 'a'

step 1: Initialize j= 1

step 2: repeat through step-8 while(j<n)

step 3: set item =a[j]

 
by Dinesh Thakur

Algorithm for Quick Sort:

Here 'a' is an array of elements and 'first' and 'last' represent the first index and last index of the array 'a' Perform the following steps only if(first<last).

step 1: Set i=first

step 2: Set j=last+l

step 3: Set k=first

 
by Dinesh Thakur

Algorithm for Bubble Sort:

step 1: read num

step 2: create an integer array a[] of size num

step 3: set flag =1

step 4: initialize i=0

step 5: repeat through step-11 while (i <n) and flag=l

step 6: Reset flag=0

 
by Dinesh Thakur

Algorithm for Removing duplicate elements from Array:

step 1: read y

step 2: create two integer array a[],b[] of size y

step 3: initialize c=0

step 4: repeat through step-6 while c less than a.length

step 5: read a[c]

step 6: c=c+1

step 7: initialize c=0

 
by Dinesh Thakur

Algorithm for Sum and Average the Elements:

step 1: set sum=0

step 2: read arr_size

step 3: create an integer array arr[] of size arr_size

step 4: initialize i=0

 
by Dinesh Thakur

Algorithm for Largest Number in an Array:

 

step 1: set big=0,small=0

step 2: read arr_size

step 3: create an integer array a[] of size arr_size

step 4: initialize x=0

step 5: repeat through step-7 while x less than a.length

step 6: read a[x]

 
by Dinesh Thakur

Algorithm for SortinganArray:

step 1: set temp=0

step 2: read arr_size

step 3: create an integer array a[] of size arr_size

step 4: initialize i=0

step 5: repeat through step-11 while i less than a.length

step 6: read a[i]

 
by Dinesh Thakur

Algorithm for Ones Present Inputted Array:

step 1: set arr_size,j, count as three static variables of integer type

step 2: read arr_size

step 3: create an integer array arr[] of size arr_size

step 4: initialize i=0 

 
by Dinesh Thakur

Here is the Java Example for Even-Odd pairs within that number:



 
by Dinesh Thakur

Explanation of Math.pow():

Pow()method is a static method of math class present in java.lang package.

This method takes two double type argument and returns the value if the 1st argument which is raised to the power of the 2nd argument.



 
by Dinesh Thakur

Algorithm for Convert Decimal integer to Binary Number:
step 1: set i=0
step 2: read num
step 3: create two integer arrays a[],b[] of size 20
step 4: repeat through step- 7 while num greater than 0

 
by Dinesh Thakur

Algorithm for Convert decimal integer to octal Number:

step 1: set r=0, i=0

step 2: create two integer arrays a[],b[] of size 10

step 3: read num

 
by Dinesh Thakur

Array is a collection of similar elements. Array is a predefined class present in java.1ang package. It is a final class which can't be inherited. In java array holds both primitive data types as well as object references. In java when the programmer develop the array program then the programmer has to follow 3-steps.



 
by Dinesh Thakur

Array is a group of related data items, that share a common name with same data type. An individual variable in the array is called an array element.

We can define an array name rollno to represent a set of roll no of a group of students. A particular value is indicated by a number called index number. An index number is present in brackets after the array name.