0% found this document useful (0 votes)
16 views

unit 2 java

Uploaded by

manthantiwariwd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

unit 2 java

Uploaded by

manthantiwariwd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Class and objects

Unit 2
What is class?
In object-oriented programming, a class is a basic building block. It can be defined as
template that describes the data and behaviour associated with the class instantiation.
Instantiating is a class is to create an object (variable) of that class that can be used to
access the member variables and methods of the class. A class can also be called a logical
template to create the objects that share common properties and methods. For example,
an Employee class may contain all the employee details in the form of variables and
methods. If the class is instantiated i.e. if an object of the class is created (say e1), we can
access all the methods or properties of the class.
Java provides a reserved keyword class to define a class. The keyword must be followed by
the class name. Inside the class, we declare methods and variables.
In general, class declaration includes the following in the order as it appears:
1.Modifiers: A class can be public or has default access.
2.class keyword: The class keyword is used to create a class.
3.Class name: The name must begin with an initial letter (capitalized by convention).
4.Superclass (if any): The name of the class's parent (superclass), if any, preceded by the
keyword extends. A class can only extend (subclass) one parent.
5.Interfaces (if any): A comma-separated list of interfaces implemented by the class, if
any, preceded by the keyword implements. A class can implement more than one
interface.
Syntax of declaring a class:
<access specifier> class class_name
{
// member variables
// class methods
}
What are Instance Variables?
Instance variables are variables that are declared in a class but outside of any
constructor, method, or block. They are created when an object is instantiated, and
are accessible to all the constructors, methods, or blocks in the class. Access
modifiers can be given to the instance variable.
Instance variables are also known as member variables or fields. They are used to
store data that is specific to each instance of the class. For example, a class
called student might have instance variables for the student's name, age, and
GPA. Each instance of the student class would have its own copy of these instance
variables, with values that are specific to that student.
What are class Variables?
In Java, class variables, also known as static variables, are variables that are declared
with the static Static variables are accessed using the class name (e.g.,
Myclass.count) rather than through instances of the class, although it's possible to
access them through an instance. However, this is not recommended since it can be
misleading and may give the impression that the variable belongs to the instance
rather than to the class itself.
What is a method in Java?
A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. It is used to
achieve the reusability of code. We write a method once and use it many times. We do not require to write code again and again. It also
provides the easy modification and readability of code, just by adding or removing a chunk of code. The method is executed only when we call
or invoke it.
The most important method in Java is the main() method.
Method Declaration
The method declaration provides information about method attributes, such as visibility, return-type, name, and arguments.
• Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of the method. Java
provides four types of access specifier:
• Public: The method is accessible by all classes when we use public specifier in our application.
• Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
• Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different
package.
• Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is visible
only from the same package only.
• Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method
does not return anything, we use void keyword.
• Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the method.
Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its
name.
• Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and
variable name. If the method has no parameter, left the parentheses blank.
• Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces.
Types of method:
Predefined Method:
In Java, predefined methods are the method that is already defined in the Java
class libraries is known as predefined methods. It is also known as the standard
library method or built-in method. We can directly use these methods just by
calling them in the program at any point. Some pre-defined methods are length(),
equals(), compareTo(), sqrt(), etc. When we call any of the predefined methods in
our program, a series of codes related to the corresponding method runs in the
background that is already stored in the library.
User-defined Method:
The method written by the user or programmer is known as a user-
defined method. These methods are modified according to the requirement. Once
we have defined a method, it should be called. The calling of a method in a
program is simple. When we call or invoke a user-defined method, the program
control transfer to the called method.
Static Method:
A method that has static keyword is known as static method. In
other words, a method that belongs to a class rather than an
instance of a class is known as a static method. We can also
create a static method by using the keyword static before the
method name. The main advantage of a static method is that we
can call it without creating an object. It can access static data
members and also change the value of it. It is used to create an
instance method. It is invoked by using the class name. The best
example of a static method is the main() method.
Instance Method:
The method of the class is known as an instance method. It is
a non-static method defined in the class. Before calling or
invoking the instance method, it is necessary to create an object
of its class. Let's see an example of an instance method.
public class InstanceMethodExample
{
public static void main(String [] args)
{
//Creating an object of the class
InstanceMethodExample obj = new InstanceMethodExample();
//invoking instance method
System.out.println("The sum is: "+obj.add(12, 13));
}
int s;
//user-defined method because we have not used static keyword
public int add(int a, int b)
{
s = a+b;
//returning the sum
return s; Output:
} The sum is: 25
What are Constructors in Java?
In Java, a Constructor is a block of codes similar to the method. It is called when an
instance of the class is created. At the time of calling the constructor, memory for the
object is allocated in the memory. It is a special type of method that is used to
initialize the object. Every time an object is created using the new() keyword, at least
one constructor is called.
Note: It is not necessary to write a constructor for a class. It is because the java
compiler creates a default constructor (constructor with no arguments) if your class
doesn’t have any.
How Java Constructors are Different From Java Methods?
• Constructors must have the same name as the class within which it is defined it is
not necessary for the method in Java.
• Constructors do not return any type while method(s) have the return type or void if
does not return any value.
• Constructors are called only once at the time of Object creation while method(s)
Types of constructor in java
1. Default Constructor
A constructor that has no parameters is known as default the constructor. A default
constructor is invisible. And if we write a constructor with no arguments, the compiler
does not create a default constructor. It is taken out. It is being overloaded and called a
parameterized constructor. The default constructor changed into the parameterized
constructor. But Parameterized constructor can’t change the default constructor.
2. Parameterized Constructor
A constructor that has parameters is known as parameterized constructor. If we want to
initialize fields of the class with our own values, then use a parameterized constructor.
3. Copy Constructor
Unlike other constructors copy constructor is passed with another object which copies
the data available from the passed object to the newly created object.
Note: In Java,there is no such inbuilt copy constructor available like in other
programming languages such as C++, instead we can create our own copy constructor
by passing the object of the same class to the other instance(object) of the class.
Example of copy constructor:
import java.io.*;

class Geek {
// data members of the class.
String name;
int id;

// Parameterized Constructor
Geek(String name, int id)
{
this.name = name;
this.id = id;
}

// Copy Constructor
Geek(Geek obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
lass GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Geek geek1 = new Geek("avinash", 68);
Output
System.out.println("GeekName :" + geek1.name First Object GeekName :avinash and GeekId :68
+ " and GeekId :" + geek1.id); Copy Constructor used Second Object GeekName :avinash and GeekId
:68
System.out.println();

// This would invoke the copy constructor.


Geek geek2 = new Geek(geek1);
System.out.println( "Copy Constructor used Second Object");
System.out.println("GeekName :" + geek2.name
+ " and GeekId :" + geek2.id);
}
Method Overloading in Java
Method overloading in Java refers to the capability of a class to define multiple methods with the same name but
with different parameters. This allows developers to create methods with similar functionalities but with variations
in the number or types of parameters they accept.
Example:
public class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two doubles
public double add(double a, double b) {
return a + b;
}
Different ways to overload the method
There are two ways to overload the method in java
1) Method Overloading: changing no. of arguments
In this example, we have created two methods, first add() method performs addition of two
numbers and second add method performs addition of three numbers.
In this example, we are creating static methods so that we don't need to create instance for
calling methods.
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
} Outpu
class TestOverloading1{ t:
public static void main(String[] args){ 22
System.out.println(Adder.add(11,11)); 33
System.out.println(Adder.add(11,11,11));
2) Method Overloading: changing data type of arguments
In this example, we have created two methods that differs in
data type. The first add method receives two integer arguments
and second add method receives two double arguments.
class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
Output:
System.out.println(Adder.add(12.3,12.6));
22
}} 24.9
What is Constructor overloading
Constructor overloading in Java refers to the capability of a class to have multiple constructors with different parameter lists. Just like
method overloading, constructor overloading allows developers to create constructors with different sets of parameters, enabling flexibility
in object initialization
Here, constructors
Consider the following Java program, in which we have used different we needin the to class.
understand the purpose of
public class Student { constructor overloading. Sometimes, we need to
//instance variables of the class use multiple constructors to initialize the different
int id; values of the class.
String name; We must also notice that the java compiler invokes
Student(){ a default constructor when we do not use any
System.out.println("this a default constructor"); constructor in the class. However, the default
} constructor is not invoked if we have used any
Student(int i, String n){ constructor in the class, whether it is default or
id = i;
parameterized. In this case, the java compiler
name = n;
}
throws an exception saying the constructor is
Output:
public static void main(String[] args) { undefined.
this a default constructor Default
//object creation Constructor values
Student s = new Student(); Student Id : 0
System.out.println("\nDefault Constructor values: \n"); Student Name : null
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name); Parameterized Constructor values:
System.out.println("\nParameterized Constructor values: \n"); Student Id : 10
Student student = new Student(10, "David"); Student Name : David
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
}
What are Arrays in Java?
Java provides a data structure called the array, which stores a fixed-size sequential collection of elements of the same data type. An array is
used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and
use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
Declaring Array Variables:-
To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can
reference. Here is the syntax for declaring an array variable −
Syntax:
datatype[] arrayrefvar; //preferred way
Or
datatype arrayrefvar[]; //works but not preferred way
Creating Arrays:
You can create an array by using the new operator with the following syntax −
arrayrefvar = new datatype[arraysize];
The above statement does two things −
• It creates an array using new dataType[arraySize].
• It assigns the reference of the newly created array to the variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as
shown below −
datatype[] arrayrefvar = new datatype[arraysize];
Alternatively you can create arrays as follows −
datatype[] arrayrefvar = {value1, value2…….,valuek};
Example:
Following statement declares an array variable, myList, creates an
array of 10 elements of double type and assigns its reference to
myList −
double[] mylist = new double[10];
Processing Arrays
When processing array elements, we often use either for loop
or foreach loop because all of the elements in an array are of the
same type and the size of the array is known.
Example:
Here is a complete example showing how to create, initialize, and
process arrays −
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i]; Output:
} 1.9
System.out.println("Total is " + total); 2.9
// Finding the largest element
3.4
double max = myList[0];
3.5
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
Total is 11.7
} Max is 3.5
System.out.println("Max is " + max);
}
The foreach Loops:
JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which
enables you to traverse the complete array sequentially without using an index
variable.
Example
The following code displays all the elements in the array myList −
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements Output:
for (double element: myList) { 1.9
System.out.println(element); 2.9
3.4
} 3.5
}
The java.util.Arrays class contains various static methods for
sorting and searching arrays, comparing arrays, and filling array
elements. These methods are overloaded for all primitive types.
Sr.No. Method & Description
public static int binarySearch(Object[] a, Object key)
Searches the specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary search
1
algorithm. The array must be sorted prior to making this call. This returns index of the search key, if it is
contained in the list; otherwise, it returns ( – (insertion point + 1)).

public static boolean equals(long[] a, long[] a2)


Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if
2 both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are
equal. This returns true if the two arrays are equal. Same method could be used by all other primitive data types
(Byte, short, Int, etc.)
public static void fill(int[] a, int val)
3 Assigns the specified int value to each element of the specified array of ints. The same method could be used by
all other primitive data types (Byte, short, Int, etc.)
public static void sort(Object[] a)
4 Sorts the specified array of objects into an ascending order, according to the natural ordering of its elements.
The same method could be used by all other primitive data types ( Byte, short, Int, etc.)
What is string?
String is an object that represents sequence of characters. In Java, String is represented by String class which is located into
java.lang package. It is probably the most commonly used class in java library. In java, every string that we create is actually an
object of type String. One important thing to notice about string object is that string objects are immutable that means once a
string object is created it cannot be changed. The Java String class implements Serializable, Comparable and CharSequence
interface that we have represented using the below image.

What is an Immutable object?


An object whose state cannot be changed after it is created is known as an Immutable object. String, Integer, Byte, Short, Float,
Double and all other wrapper classes objects are immutable.
Creating strings:
String can be created in number of ways, here are a few ways of creating string object.
1) Using a String literal
String literal is a simple string enclosed in double quotes " ". A string literal is treated as a String object.
public class Demo
{
public static void main(String[] args)
{
String s1 = "Hello Java";//String Literal
System.out.println(s1);
}
}
2) Using new Keyword
We can create a new string object by using new operator that allocates memory for the object.
public class Demo{
public static void main(String[] args) {
String s1 = new String("Hello Java");
System.out.println(s1);
}
Each time we create a String literal, the JVM checks the string pool first. If the string literal already exists in the pool, a reference
to the pool instance is returned. If string does not exist in the pool, a new string object is created, and is placed in the pool. String
objects are stored in a special memory area known as string constant pool inside the heap memory.

Here are some method of string:


Sr.No. Method & Description
1 char charAt(int index)This method returns the char value at the specified index.
2 int codePointAt(int index)This method returns the character (Unicode code point) at the specified index.

3 int codePointBefore(int index)This method returns the character (Unicode code point) before the specified index.

4 int codePointCount(int beginIndex, int endIndex)This method returns the number of Unicode code points in the specified text range of
this String.

5 int compareTo(String anotherString)This method compares two strings lexicographically.

6 int compareToIgnoreCase(String str)This method compares two strings lexicographically, ignoring case differences.

7 String concat(String str)This method concatenates the specified string to the end of this string.

8 boolean contains(CharSequence s)This method ceturns true if and only if this string contains the specified sequence of char values.

9 boolean contentEquals(CharSequence cs)This method compares this string to the specified CharSequence.

10 boolean contentEquals(StringBuffer sb)This method compares this string to the specified StringBuffer.
11 static String copyValueOf(char[] data)This method returns a String that represents the character sequence in the array specified.

12 static String copyValueOf(char[] data, int offset, int count)This method returns a String that represents the character sequence in the
array specified.
13 boolean endsWith(String suffix)This method tests if this string ends with the specified suffix.
14 boolean equals(Object anObject)This method compares this string to the specified object.

15 boolean equalsIgnoreCase(String anotherString)This method compares this String to another String, ignoring case considerations.

16 static String format(Locale l, String format, Object... args)This method returns a formatted string using the specified locale, format
string, and arguments.

17 static String format(String format, Object... args)This method returns a formatted string using the specified format string and
arguments.

18 byte[] getBytes()This method encodes this String into a sequence of bytes using the platform's default charset, storing the result into a
new byte array.

19 byte[] getBytes(Charset charset)This method encodes this String into a sequence of bytes using the given charset, storing the result
into a new byte array.

20 byte[] getBytes(String charsetName)This method encodes this String into a sequence of bytes using the named charset, storing the
result into a new byte array.

21 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)This method copies characters from this string into the destination
character array.
22 int hashCode()This method returns a hash code for this string.
23 int indexOf(int ch)This method returns the index within this string of the first occurrence of the specified character.

24 int indexOf(int ch, int fromIndex)This method returns the index within this string of the first occurrence of the specified character,
starting the search at the specified index.
int indexOf(String str, int fromIndex)This method returns the index within this string of the first occurrence of the specified substring,
26 starting at the specified index.
27 String intern()This method returns a canonical representation for the string object.
28 boolean isEmpty()This method returns true if, and only if, length() is 0.
29 int lastIndexOf(int ch)This method returns the index within this string of the last occurrence of the specified character.
int lastIndexOf(int ch, int fromIndex)This method returns the index within this string of the last occurrence of the specified character,
30 searching backward starting at the specified index.
31 int lastIndexOf(String str)This method returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)This method returns the index within this string of the last occurrence of the specified substring,
32 searching backward starting at the specified index.
33 int length()This method returns the length of this string.
34 boolean matches(String regex)This method tells whether or not this string matches the given regular expression.
int offsetByCodePoints(int index, int codePointOffset)This method returns the index within this String that is offset from the given index
35 by codePointOffset code points.

36 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)This method tests if two string regions are
equal with case ignored.
37 boolean regionMatches(int toffset, String other, int ooffset, int len)This method tests if two string regions are equal.
String replace(char oldChar, char newChar)This method returns a new string resulting from replacing all occurrences of oldChar in this
38 string with newChar.
String replace(CharSequence target, CharSequence replacement)This method replaces each substring of this string that matches the
39 literal target sequence with the specified literal replacement sequence.
String replaceAll(String regex, String replacement)This method replaces each substring of this string that matches the given regular
40 expression with the given replacement.
String replaceFirst(String regex, String replacement)This method replaces the first substring of this string that matches the given regular
41 expression with the given replacement.
42 String[] split(String regex)This method splits this string around matches of the given regular expression.
43 String[] split(String regex, int limit)This method splits this string around matches of the given regular expression.
44 boolean startsWith(String prefix)This method tests if this string starts with the specified prefix.

45 boolean startsWith(String prefix, int toffset)This method tests if the substring of this string beginning at the specified index starts with
the specified prefix.
CharSequence subSequence(int beginIndex, int endIndex)This method returns a new character sequence that is a subsequence of this
46 sequence.
47 String substring(int beginIndex)This method returns a new string that is a substring of this string.

48 String substring(int beginIndex, int endIndex)This method returns a new string that is a substring of this string.
49 char[] toCharArray()This method converts this string to a new character array.
50 String toLowerCase()This method converts all of the characters in this String to lower case using the rules of the default locale.

51 String toLowerCase(Locale locale)This method converts all of the characters in this String to lower case using the rules of the given Locale.
52 String toString()This method returns the string itself.
53 String toUpperCase()This method converts all of the characters in this String to upper case using the rules of the default locale.

54 String toUpperCase(Locale locale)This method converts all of the characters in this String to upper case using the rules of the given
Locale.
55 String trim()This method returns a copy of the string, with leading and trailing whitespace omitted.
56 static String valueOf(boolean b)This method returns the string representation of the boolean argument.

57 static String valueOf(char c)This method returns the string representation of the char argument.

58 static String valueOf(char[] data)This method returns the string representation of the char array argument.

59 static String valueOf(char[] data, int offset, int count)This method Returns the string representation of a specific subarray of the char
array argument.

60 static String valueOf(double d)This method returns the string representation of the double argument.

61 static String valueOf(float f)This method returns the string representation of the float argument.

62 static String valueOf(int i)This method returns the string representation of the int argument.

63 static String valueOf(long l)This method returns the string representation of the long argument.

64 static String valueOf(Object obj)This method returns the string representation of the Object argument.
What are vectors?
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we
can store n-number of elements in it as there is no size limit. It is a part of Java
Collection framework since Java 1.2. It is found in the java.util package and
implements the List interface, so we can use all the methods of List interface here
It is recommended to use the Vector class in the thread-safe implementation only.
If you don't need to use the thread-safe implementation, you should use the
ArrayList, the ArrayList will perform better in such case.
Vector implements a dynamic array. It is similar to ArrayList, but with two
differences −
• Vector is synchronized.
• Vector contains many legacy methods that are not part of the collections
framework.
Vector proves to be very useful if you don't know the size of the array in advance
or you just need one that can change sizes over the lifetime of a program.
Java Vector Constructors
Vector class supports four types of constructors. These are given
below:

SN Constructor Description
1) vector() It constructs an empty vector
with the default size as 10.
2) vector(int initialCapacity) It constructs an empty vector
with the specified initial
capacity and with its capacity
increment equal to zero.
3) vector(int initialCapacity, int It constructs an empty vector
capacityIncrement) with the specified initial
capacity and capacity
increment.
4) Vector( Collection<? extends E> It constructs a vector that
c) contains the elements of a
collection c.
Example:
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
//Create a vector
Vector<String> vec = new Vector<String>();
//Adding elements using add() method of List
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
//Adding elements using addElement() method of Vector
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
Output:
System.out.println("Elements are: "+vec);
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat,
} Deer]
The following are the list of Vector class
methods:
SN Method Description
1) add() It is used to append the specified element in the given vector.

2) addAll() It is used to append all of the elements in the specified collection to the end of this
Vector.

3) addElement() It is used to append the specified component to the end of this vector. It increases the
vector size by one.

4) capacity() It is used to get the current capacity of this vector.

5) clear() It is used to delete all of the elements from this vector.

6) clone() It returns a clone of this vector.


7) contains() It returns true if the vector contains the specified element.

8) containsAll() It returns true if the vector contains all of the elements in the specified collection.

9) copyInto() It is used to copy the components of the vector into the specified array.

10) elementAt() It is used to get the component at the specified index.

11) elements() It returns an enumeration of the components of a vector.


12) ensureCapacity() It is used to increase the capacity of the vector which is in use, if necessary. It ensures
that the vector can hold at least the number of components specified by the minimum
capacity argument.
13) equals() It is used to compare the specified object with the vector for equality.
14) firstElement() It is used to get the first component of the vector.
15) forEach() It is used to perform the given action for each element of the Iterable until all elements
have been processed or the action throws an exception.
16) get() It is used to get an element at the specified position in the vector.
17) hashCode() It is used to get the hash code value of a vector.
18) indexOf() It is used to get the index of the first occurrence of the specified element in the vector. It
returns -1 if the vector does not contain the element.
19) insertElementAt() It is used to insert the specified object as a component in the given vector at the
specified index.
20) isEmpty() It is used to check if this vector has no components.
21) iterator() It is used to get an iterator over the elements in the list in proper sequence.
22) lastElement() It is used to get the last component of the vector.
23) lastIndexOf() It is used to get the index of the last occurrence of the specified element in the vector. It
returns -1 if the vector does not contain the element.
24) listIterator() It is used to get a list iterator over the elements in the list in proper sequence.
25) remove() It is used to remove the specified element from the vector. If the vector does not contain
the element, it is unchanged.
26) removeAll() It is used to delete all the elements from the vector that are present in the specified
collection.
27) removeAllElemen It is used to remove all elements from the vector and set the size of the vector to zero.
ts()
28) removeElement() It is used to remove the first (lowest-indexed) occurrence of the argument from the
vector.
31) removeRange It is used to delete all of the elements from the vector whose index is between fromIndex,
() inclusive and toIndex, exclusive.

32) replaceAll() It is used to replace each element of the list with the result of applying the operator to that
element.
33) retainAll() It is used to retain only that element in the vector which is contained in the specified
collection.
34) set() It is used to replace the element at the specified position in the vector with the specified
element.
35) setElementAt( It is used to set the component at the specified index of the vector to the specified object.
)
36) setSize() It is used to set the size of the given vector.
37) size() It is used to get the number of components in the given vector.

38) sort() It is used to sort the list according to the order induced by the specified Comparator.

39) spliterator() It is used to create a late-binding and fail-fast Spliterator over the elements in the list.

40) subList() It is used to get a view of the portion of the list between fromIndex, inclusive, and toIndex,
exclusive.
41) toArray() It is used to get an array containing all of the elements in this vector in correct order.

42) toString() It is used to get a string representation of the vector.


43) trimToSize() It is used to trim the capacity of the vector to the vector's current size.

You might also like