Java: Add value to a vector which is inside another vector.

Sometimes what we do in java programming, we add value to a vector and then adds that vector to another vector (Vector inside another vector).

For example

(“X”,”Y”,”Z”) is in Vector    name

name.add(“X”);

name.add(“Y”);

name.add(“Z”);

(“M”, “N”,”O”) is in Vector name1

name1.add(“M”);

name1.add(“N”);

name1.add(“O”);

Add  this  name and  name1 vector in nameContainer Vector

nameContainer.add(name);

nameContainer.add(name1);

So, if you want to add another value  or    modify  the vector  name and name1  which are inside nameContainer Vector,  do the following  according to the given code.

Code Example

 import java.util.Vector;

public class VectorDemo

{

 VectorDemo()

{

Vector name=new Vector();

Vector name1=new Vector();

Vector name2=new Vector();

name2.add(“Its me”);

Vector nameContainer=new Vector();

name.add(“X”);

name.add(“Y”);

name.add(“Z”);

nameContainer.add(name);

name1.add(“M”);

name1.add(“N”);

name1.add(“O”);

nameContainer.add(name1);

System.out.println(“Before NameContainer Value:”+nameContainer.get(0));

((Vector)nameContainer.get(0)).add(“HI”);

System.out.println(“name get 0 and 1:”+name.get(0)+” “+name.get(1));

System.out.println(“AfterNameContainer Value:”+nameContainer.get(0));

((Vector)nameContainer.get(0)).add(name2.get(0));

System.out.println(“AfterNameContainer Value:”+nameContainer.get(0).toString());

}

public static void main(String args[])

{

VectorDemo x=new VectorDemo();

}

}

OUTPUT:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: