Array:: Import Java - Util. Public Class Array (

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Array: Array is the most important thing in any programming language.

By definition, array is the static


memory allocation. It allocates the memory for the same data type in sequence. It contains multiple
values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are
used in any programming language such as: one - dimensional, two - dimensional or can say multi -
dimensional.

Declaration of an array:
int num[]; or int num = new int[2];
Some times user declares an array and it's size simultaneously. You may or may not be define the size in
the declaration time. such as:
int num[] = {50,20,45,82,25,63};

In this program we will see how to declare and implementation. This program illustrates that the array
working way. This program takes the numbers present in the num[] array in unordered list and prints
numbers in ascending order. In this program the sort()function of the java.util.*; package is using to sort
all the numbers present in the num[] array. The Arrays.sort() automatically sorts the list of number in
ascending order by default. This function held the argument which is the array name num.

Here is the code of the program:-


import java.util.*;

public class array{


public static void main(String[] args){
int num[] = {50,20,45,82,25,63};
int l = num.length;
int i,j,t;
System.out.print("Given number : ");
for (i = 0;i < l;i++ ){
System.out.print(" " + num[i]);
}
System.out.println("\n");
System.out.print("Accending order number : ");
Arrays.sort(num);
for(i = 0;i < l;i++){
System.out.print(" " + num[i]);
}
}
}
Introduction to Vector
Vector are array list with extended properties which follow the dynamic and automatic addition of data at
run-time. Unlike array, Vector can grow and shrink as needed to accommodate its size adding and
removing items even after the Vector has been created. The Vector class implements dynamic array of
objects. The Component in vector class can be accessed by integer index. Each vector tries to maintain
its storage size by capacity and a capacity Increment. The capacity of vector is always least as of its
size and usually larger because components are added to the vector at run-time. The vector's storage
get increase in chunks as the size of capacity Increment. An application increase the size of a vector
before inserting a large number of components that is used to reduce the amount of increment in
reallocation. Vectors can be easily used.. It implement the Enumeration interface which makes the
contents of a Vector traversed

Vector instances are like linked-lists that can grow dynamically. Once it reaches the maximum limit,
resources are allocated dynamically and increases its size further.

How to declare Vector

Vector

The given below Syntax is used to declare an empty vector so that the size of internal data array has 10
and its standard storage capacity has zero.

public Vector()

The given Syntax is used to construct an empty vector specified by initial capacity.

vector(int initial capacity)

The below Syntax Creates an empty vector with the specified initial capacity and capacity increment.

vector(int initial capacity,int capacity increment)

Method used in Vector

void add(int index, Object element)- Inserts the specified element at the specified position in this Vector

.
boolean add(object o)- Appends the specified element to the end of this Vector.

int capacity( ) - Return the current capacity of this vector

void clear( )- Removes all of the elements from this Vector.


void setsize(int new size )-Set the new size of Vector.

int size( )-Return the number of Component in Vector.

Vector trim to size( )- Trim the Capacity of Vector to vector's current size.

public int lastindexof(obj abc)-Returns the occurrence of last index of of the specified object in this vector.

Understand Vector by Example-

This java example shows us to find a maximum element of Java Vector using max method of Collections
class. We Import required package, then declare a Class name maximum vector element .Inside the main
method create an object of vector. The object of vector class calls add method to add the component of
vector. In order to find the maximum element of vector component use, static object max(collection c)
method of collection class which we already imported the Collection package .This method returns the
maximum element of Java Vector.

import java.util.Vector;
import java.util.Collections;

public class maximum vector element


{

public static void main(String[] args)

//create a Vector object


Vector v = new Vector();

//Add elements to Vector


v.add(new int("500"));
v.add(new int("745"));
v.add(new int("842"));
v.add(new int("900"));
v.add(new int("567"));

Object obm = Collections.max(v);

System.out.println("Maximum Element of Java Vector is : " + obm);


}
}
String class is belongs to the java.lang package. It is a final class i.e. it can not be extended. Because the
String class is defined as final so its object are immutable however, String buffers are mutable. This class
has defined various of methods to perform operations on strings. Some of these methods are as follows :

 char charAt(int index) : This method is used to get the character at the given index of a given
String.

Syntax : public char charAt(int index)

 int codePointAt(int index) : This method is used to find the Unicode of character at the given
index of a String.

Syntax : public int codePointAt(int index)

 int codePointBefore(int index) : This method is used to find the Unicode of character before the
given index of a String.

Syntax : public int codePointBefore(int index)

 int codePointCount(int beginIndex, int endIndex) : This method is used to get the number of
Unicode points in the given text range of index of a String.

Syntax : public int codePointCount(int beginIndex, int endIndex)

 int compareTo(String anotherString) : This method compares two strings using Unicode value
of every characters. It compares the sequence of characters of Strings lexicographically.

Syntax : public int compareTo(String anotherString)

 int compareToIgnoreCase(String str) : This method compares two strings using Unicode value
of every characters. It compares the sequence of characters of Strings lexicographically by
ignoring case. The returned value of this method may be a negative integer, zero, or a positive
integer that demonstrates the comparing string is greater than, equal to or, less than the
compared String respectively.
Syntax : public int compareToIgnoreCase(String str)

 String concat(String str) : This method is used to append the given string at the end of the
string.

Syntax : public String concat(String str)

 boolean contains(CharSequence s) : This method is used to check whether the given


sequence of char values is contained by the current String or not.

Syntax : public boolean contains(CharSequence s)

 boolean contentEquals(CharSequence cs) : This method is used to compare the String with
the given sequence of char.

Syntax : public boolean contentEquals(CharSequence cs)

 boolean contentEquals(StringBuffer sb) : This method is used to compare the String with the
given StringBuffer.

Syntax : public boolean contentEquals(StringBuffer sb)

 static String copyValueOf(char[] data) : This method is used to get the String from the given
array of characters.

Syntax : public static String copyValueOf(char[] data)

 static String copyValueOf(char[] data, int offset, int count) : This method is used to get the
String from the given array of characters started from the specified index and the number of
characters.

Syntax : public static String copyValueOf(char[] data, int offset, int count)

 boolean endsWith(String suffix) : This method is used to check whether the string is ends with
the given string.
Syntax : public boolean endsWith(String suffix)

 boolean equals(Object anObject) : This method is used to check whether the String object is
equal to the given Object or not.

Syntax : public boolean equals(Object anObject)

 boolean equalsIgnoreCase(String anotherString) : This method is used to check by ignoring


the case of characters, whether the String object is equal to the the given String object or not.

Syntax : public boolean equalsIgnoreCase(String anotherString)

 static String format(Locale l, String format, Object... args) : This method is used to format a
String using the given Locale, format string and arguments.

Syntax : public static String format(Locale l, String format, Object... args)

 static String format(String format, Object... args) : This method is used to format a String
using the given format string and arguments.

Syntax : public static String format(String format, Object... args)

 byte[] getBytes() : This method is used to get the array of bytes after encoding the String.

Syntax : public byte[] getBytes()

 byte[] getBytes(Charset charset) : This method is used to get the array of byte after encoding
the String with the given charset.

Syntax : public byte[] getBytes(Charset charset)

 int hashCode() : This method is used to get the hash code of String.

Syntax : public int hashCode()


 int indexOf(int ch) : This method is used to get the position of the first occurrence of given
character inside the String.

Syntax : public int indexOf(int ch)

 int indexOf(int ch, int fromIndex) : This method is used to get the position of the first
occurrence of given character inside the String. In this method search operation is started from
the given position.

Syntax : public int indexOf(int ch, int fromIndex)

 int indexOf(String str) : This method is used to get the position of the first occurrence of given
substring inside the String.

Syntax : public int indexOf(String str)

 int indexOf(String str, int fromIndex) : This method is used to get the position of the first
occurrence of given substring inside the String. In this method search operation is started from
the given position.

Syntax : public int indexOf(String str, int fromIndex)

 boolean isEmpty() : This method is used to check whether the String has 0 (zero) length or not.

Syntax : public boolean isEmpty()

 int lastIndexOf(int ch) : This method is used to get the last occurrence position of the given
character inside a String.

Syntax : public int lastIndexOf(int ch)

 int lastIndexOf(int ch, int fromIndex) : This method is used to get the last occurrence position
of the given character inside a String. In this method the search is started from the given index.

Syntax : public int lastIndexOf(int ch, int fromIndex)


 int lastIndexOf(String str) : This method is used to get the last occurrence position of the given
substring inside a String.

Syntax : public int lastIndexOf(String str)

 int lastIndexOf(String str, int fromIndex) : This method is used to get the last occurrence
position of the given substring inside a String. In this method the search operation is started from
the given index.

Syntax : public int lastIndexOf(String str, int fromIndex)

 int length() : This method is used to get the number of characters within the String.

Syntax : public int length()

 boolean matches(String regex) : This method is used to match the string with the given regular
expression.

Syntax : public boolean matches(String regex)

 String replace(char oldChar, char newChar) : This method is used to replace the given old
character with the given new character.

Syntax : public String replace(char oldChar, char newChar)

 String replaceAll(String regex, String replacement) : This method is used to replace all
substring of the String if the given regular expression is matched with the given replacement.

Syntax : public String replaceAll(String regex, String replacement)

 String[] split(String regex) : This method is used to split the string from the specified matching
regular expression.

Syntax : public String[] split(String regex)


 String[] split(String regex, int limit) : This method is used to split the string form the specified
matching regular expression with the specified limit.

Syntax : public String[] split(String regex, int limit)

 boolean startsWith(String prefix) : This method is used to check whether the string is started
with the given string or not.

Syntax : public boolean startsWith(String prefix)

 String substring(int beginIndex) : This method is used to get the string within the String called
substring. Substring is started from the given index.

Syntax : public String substring(int beginIndex)

 String substring(int beginIndex, int endIndex) : This method is used to get the string within the
String called substring. This Substring is a part of the string specified within the starting index and
end index.

Syntax : public String substring(int beginIndex, int endIndex)

 char[] toCharArray() : This method is used to convert a string into an array of characters.

Syntax : public char[] toCharArray()

 String toLowerCase() : This method is used to convert the upper case characters to lower case.

Syntax : public String toLowerCase()

 String toString() : This method is used to get the String itself.

Syntax : public String toString()

 String toUpperCase() : This method is used to convert the lower case characters to upper case.
Syntax : public String toUpperCase()

 String trim() : This method is used to omit the whitespace around the String.

Syntax : public String trim()

 static String valueOf(boolean b) : This method is used to represent a boolean as a String.

Syntax : public static String valueOf(boolean b)

 static String valueOf(Object obj) : This method is used to represent an object as a String.

Syntax : public static String valueOf(Object obj)

You might also like