by Dinesh Thakur Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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. Two-dimensional Array is specified by taking additional square brackets i.e. "[][]".

 
by Dinesh Thakur Category: array

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

 
by Dinesh Thakur Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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 Category: array

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]

 

Page 4 of 5



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles