TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Thursday, 26 December 2019

vector in java in hindi | vector program in java

Vector in Java


The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections.
·       Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index
·       They are very similar to ArrayList but Vector is synchronised and have some legacy method which collection framework does not contain.
·       It extends AbstractList and implements List interfaces.

Constructor:

·       Vector(): Creates a default vector of initial capacity is 10.
·       Vector(int size): Creates a vector whose initial capacity is specified by size.
·       Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr. It specifies the number of elements to allocate each time that a vector is resized upward.
·       Vector(Collection c): Creates a vector that contains the elements of collection c.

Important points regarding Increment of vector capacity:

If increment is specified, Vector will expand according to it in each allocation cycle but if increment is not specified then vector’s capacity get doubled in each allocation cycle. Vector defines three protected data member:

·       int capacityIncreament: Contains the increment value.

·       int elementCount: Number of elements currently in vector stored in it.

·       Object elementData[]: Array that holds the vector is stored in it.

  
 
vector in java in hindi
vector in java

Methods in Vector:

1.    boolean add(Object obj): This method appends the specified element to the end of this vector.

·       Syntax: public boolean add(Object obj)

·       Returns: true if the specified element is added

           successfully into the Vector, otherwise it returns false.

·       Exception: NA.

  
// Java code illustrating add() method
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector
        Vector v = new Vector();
  
        v.add(1);
        v.add(2);
        v.add("geeks");
        v.add("forGeeks");
v.add(3);
  
        System.out.println("Vector is " + v);
    }
}
Output:
    [1, 2, geeks, forGeeks, 3]
2.void add(int index, Object obj): This method inserts the specified element at the specified position in this Vector.

·       Syntax: public void add(int index, Object obj)

·       Returns: NA.

·       Exception: IndexOutOfBoundsException, method throws this exception

    if the index (obj position) we are trying to access is out of range
    (index  size()).
   
// Java code illustrating add() method
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector
        Vector v = new Vector();
  
        v.add(0, 1);
        v.add(1, 2);
        v.add(2, "geeks");
        v.add(3, "forGeeks");
        v.add(4, 3);
  
        System.out.println("Vector is " + v);
    }
}
Output:
    Vector is: [1, 2, geeks, forGeeks, 3]
   

3.    boolean addAll(Collection c) This method appends all of the elements

in the specified Collection to the end of this Vector.

·       Syntax: public boolean addAll(Collection c)

·       Returns: Returns true if operation succeeded otherwise false.

·       Exception: NullPointerException thrown if collection is null.

   

// Java code illustrating addAll()
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
        ArrayList arr = new ArrayList();
        arr.add(3);
        arr.add("geeks");
        arr.add("forgeeks");
        arr.add(4);
  
        // createn default vector
        Vector v = new Vector();
  
        // copying all element of array list int0 vector
        v.addAll(arr);
  
        // checking vector v
        System.out.println("vector v:" + v);
    }
}
Output:
    vector v:[3, geeks, forgeeks, 4]
   

4.    boolean addAll(int index, Collection c) This method inserts all of the elements in the specified Collection into this Vector at the specified.

position.

·           Syntax: public boolean addAll(int index, Collection c)

·           Returns: true if this list changed as a result of the call.

·           Exception: IndexOutOfBoundsException -- If the index is out of range,

·           NullPointerException -- If the specified collection is null.                          
   
// Java code illustrating addAll()
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
        ArrayList arr = new ArrayList();
        arr.add(3);
        arr.add("geeks");
        arr.add("forgeeks");
        arr.add(4);
  
        // createn default vector
        Vector v = new Vector();
  
        v.add(2);
        // copying all element of array list int0 vector
        v.addAll(1, arr);
  
        // checking vector v
        System.out.println("vector v:" + v);
    }
}
Output:
    vector v:[2, 3, geeks, forgeeks, 4]

5.    void clear() This method removes all of the elements from this vector.

·           Syntax: public void clear()

·           Returns: NA.

·           Exception: NA.

   
// Java code illustrating clear() method
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector
        Vector v = new Vector();
  
        v.add(0, 1);
        v.add(1, 2);
        v.add(2, "geeks");
        v.add(3, "forGeeks");
        v.add(4, 3);
  
        System.out.println("Vector is: " + v);
  
        // clearing the vector
        v.clear();
  
        // checking vector
        System.out.println("after clearing: " + v);
    }
}
Output:
    Vector is: [1, 2, geeks, forGeeks, 3]
    after clearing: []
   

6.    Object clone() This method returns a clone of this vector.

·           Syntax: public Object clone()

·           Returns: a clone of this ArrayList instance.

·           Exception: NA.

   
filter_none
edit
play_arrow
brightness_4
// Java code illustrating clone()
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector
        Vector v = new Vector();
  
        Vector v_clone = new Vector();
  
        v.add(0, 1);
        v.add(1, 2);
        v.add(2, "geeks");
        v.add(3, "forGeeks");
        v.add(4, 3);
  
        v_clone = (Vector)v.clone();
  
        // checking vector
        System.out.println("Clone of v: " + v_clone);
    }
}
Output:
    Clone of v: [1, 2, geeks, forGeeks, 3]
   

7. boolean contains(Object o): This method returns true if this vector contains the specified element.

·       Syntax: public boolean contains(object o)

·       Returns: true if the operation is succeeded otherwise false.

·       Exception: NA.

   
// Java code illustrating contains() method
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector
        Vector v = new Vector();
  
        v.add(1);
        v.add(2);
        v.add("geeks");
        v.add("forGeeks");
        v.add(3);
  
        // check whether vector contains "forGeeks"
        if (v.contains("forGeeks"))
            System.out.println("forGeeks exists");
    }
}
Output:
forGeeks exists
   

8.    void ensureCapacity(int minCapacity): This method increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument .

·       Syntax: public void ensureCapacity(int minCapacity)

·       Returns: NA.

·       Exception: NA.

   
// Java code illustrating ensureCapacity() method
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector of capacity 10
        Vector v = new Vector();
  
        // ensuring capacity
        v.ensureCapacity(22);
  
        // cheking capacity
        System.out.println("Minimum capacity: " + v.capacity());
    }
}
Output:
    Minimum capacity: 22
   

9.    Object get(int index):This method returns the element at the specified position in this Vector.

·           Syntax: public Object get(int index)

·           Returns: returns the element at specified positions .


·           Exception: IndexOutOfBoundsException -- if the index is out of range.

   
// Java code illustrating get() methods
import java.util.*;
class Vector_demo {
    public static void main(String[] arg)
    {
  
        // create default vector of capacity 10
        Vector v = new Vector();
  
        v.add(1);
        v.add(2);
        v.add("Geeks");
        v.add("forGeeks");
        v.add(4);
  
        // get the element at index 2
        System.out.println("element at indexx 2 is: " + v.get(2));
    }

}
Output:
    element at indexx 2 is: Geek

No comments:

Post a Comment