3:11 pm - Friday November 25, 8540

Add element to the specified position in vector void add(int index,E element)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

                                          This add(int index,E element) method is used to Insert the specified element (E) at the specified position (index) in the list.It shift next element  to the right (subtracts one from their indices). Returns the element that was removed from the vector.

Example program

import java.util.Vector;

public class VectorAdd

{

	public static void main(String[] args)

	{

		Vector<Integer> h = new Vector<Integer>(7);

		h.add(0, 2);

		h.add(1, 4);

		h.add(2, 8);

		for (Integer number : h)

		{

			System.out.println("Index :" + h.indexOf(number) + " Number: "
					+ number);

		}

		h.add(3, 10);

		System.out.println("Added numbers after insertion are :- ");

		for (Integer number : h) {

			System.out.println("Index :" + h.indexOf(number) + " Number: "
					+ number);

		}
	}

}

Output

   Index :0 Number: 2

   Index :1 Number: 4

   Index :2 Number: 8

   Added numbers after insertion are :-

   Index :0 Number: 2

   Index :1 Number: 4

   Index :2 Number: 8

   Index :3 Number: 10

BACK

Filed in: Java Examples

Comments are closed.