Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Vectors in Java
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="koondeGoda" data-source="post: 2202755" data-attributes="member: 61636"><p><strong>Here is a code that demos java.util.Vector member methods. </strong></p><p><strong>To whom intersested in J.</strong></p><p> </p><p><strong>__________________________________________________</strong></p><p> </p><p><span style="font-family: 'Courier New'">/* </span></p><p> <span style="font-family: 'Courier New'">* a program demonstrating the operation of vector member functions</span></p><p> <span style="font-family: 'Courier New'">*/</span></p><p><span style="font-family: 'Courier New'">import java.io.*;</span></p><p><span style="font-family: 'Courier New'">import java.util.Vector;</span></p><p><span style="font-family: 'Courier New'">class testVectors {</span></p><p><span style="font-family: 'Courier New'"> // main</span></p><p><span style="font-family: 'Courier New'"> public static void main(String a[]){</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> Vector v1 = new Vector();</span></p><p><span style="font-family: 'Courier New'"> processVector(v1);</span></p><p><span style="font-family: 'Courier New'"> }</span></p><p><span style="font-family: 'Courier New'"> //process the vector (format / display / manipulate)</span></p><p><span style="font-family: 'Courier New'"> static void processVector(Vector v){</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> //fill the vector</span></p><p><span style="font-family: 'Courier New'"> for ( int i=0; i < 5 ; i++)</span></p><p><span style="font-family: 'Courier New'"> v.addElement(new Integer(i)); // [0,1,2,3,4]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> System.out.println(" v = "+v);</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> Integer j = new Integer(3);</span></p><p><span style="font-family: 'Courier New'"> // returns the occurences of 3 in vector v , and the occurences from index 4</span></p><p><span style="font-family: 'Courier New'"> System.out.println(v.indexOf(j)+" "+v.indexOf(j,4));</span></p><p><span style="font-family: 'Courier New'"> // returns whether v contains 3 , and last index of 3</span></p><p><span style="font-family: 'Courier New'"> System.out.println(v.contains(j)+" "+v.lastIndexOf(j));</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"></span> <span style="font-family: 'Courier New'"> // new vector v2</span></p><p><span style="font-family: 'Courier New'"> Vector v2 = new Vector(3,4); // capacity=3 capacity increment=4</span></p><p><span style="font-family: 'Courier New'"> // fill the vector v2</span></p><p><span style="font-family: 'Courier New'"> for(int i=4 ; i <8 ; i++)</span></p><p><span style="font-family: 'Courier New'"> v2.addElement(new Integer(i)); // [4,5,6,7,8]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> // extend the size of the vector to accomodate at least specified objects.</span></p><p><span style="font-family: 'Courier New'"> v2.ensureCapacity(9); </span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> Vector v3 = new Vector(2); // creates a vec capacity of 2</span></p><p><span style="font-family: 'Courier New'"> v3.setSize(4); // size = capacity = 4 v3 = [null,null,null,null] </span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> v3.setElementAt(new Integer(9), 1); // [null,9,null,null]</span></p><p><span style="font-family: 'Courier New'"> v3.setElementAt(new Integer(3), 3); // [null,9,null,3]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> // insert a element at given position after shifting elements at positions</span></p><p><span style="font-family: 'Courier New'"> // following the giben position by one.</span></p><p><span style="font-family: 'Courier New'"> v3.insertElementAt(v3.elementAt(3),1); // [null ,3,9,null,3]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> v3.ensureCapacity(9);</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> v3.removeElement(new Integer(9)); // [null,3,null,3]</span></p><p><span style="font-family: 'Courier New'"> v3.removeElementAt( v3.size()- 2); // [null,3,3]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> System.out.println("v3 "+v3);</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> //elements() returns an Enumeration obj that enumerates all the </span></p><p><span style="font-family: 'Courier New'"> // objects in the vector</span></p><p><span style="font-family: 'Courier New'"> java.util.Enumeration ev = v3.elements();</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> while(ev.hasMoreElements())</span></p><p><span style="font-family: 'Courier New'"> System.out.println(ev.nextElement()+ "");</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> System.out.println();</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> v3.removeElementAt(0); // [3,3]</span></p><p><span style="font-family: 'Courier New'"> //add all collection v</span></p><p><span style="font-family: 'Courier New'"> v3.addAll(v); // [3,3,0,1,2,3,4]</span></p><p><span style="font-family: 'Courier New'"> // removes the contains of v2</span></p><p><span style="font-family: 'Courier New'"> v3.removeAll(v2); // [3,3,0,1,2,3]</span></p><p><span style="font-family: 'Courier New'"> // adds the collection to a specified position</span></p><p><span style="font-family: 'Courier New'"> v3.addAll(2,v) ; // [3, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3]</span></p><p><span style="font-family: 'Courier New'"> // removes the objects which are not int the collection specified</span></p><p><span style="font-family: 'Courier New'"> // in this case objects not in v2 will be removed from v3</span></p><p><span style="font-family: 'Courier New'"> v3.retainAll(v2); // [4] , intersection(v3,v2)</span></p><p><span style="font-family: 'Courier New'"> v.subList(1,3).clear(); // [0,3,4]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> Vector v4 = new Vector() , v5 ;</span></p><p><span style="font-family: 'Courier New'"> v4.addElement( new Node("Jill",23));</span></p><p><span style="font-family: 'Courier New'"> v5 = (Vector)v4.clone(); // v4 = [("Jill",23)]</span></p><p><span style="font-family: 'Courier New'"> ((Node)v4.firstElement()).age = 34; // v4 = v5 = [("Jill",34)]</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> System.out.println("v4 "+v4+" \n v5 "+v5);</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> /******************************************************************/</span></p><p><span style="font-family: 'Courier New'"> /* The method clone() should be used carefully. */</span></p><p><span style="font-family: 'Courier New'"> /* It clones the array implementing the vector */</span></p><p><span style="font-family: 'Courier New'"> /* but not THE OBJECTS OF ARRAY . */</span></p><p><span style="font-family: 'Courier New'"> /* after method finished the cloned vector includes references to */</span></p><p><span style="font-family: 'Courier New'"> /* the same objects as the vector from which it was cloned. */</span></p><p><span style="font-family: 'Courier New'"> /******************************************************************/</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> }</span></p><p><span style="font-family: 'Courier New'">}</span></p><p><span style="font-family: 'Courier New'">class Node {</span></p><p><span style="font-family: 'Courier New'"> String name;</span></p><p><span style="font-family: 'Courier New'"> int age;</span></p><p><span style="font-family: 'Courier New'"> </span></p><p><span style="font-family: 'Courier New'"> Node(String s , int n){</span></p><p><span style="font-family: 'Courier New'"> this.name=s;</span></p><p><span style="font-family: 'Courier New'"> this.age=n;</span></p><p><span style="font-family: 'Courier New'"> }</span></p><p><span style="font-family: 'Courier New'"> public String toString(){</span></p><p><span style="font-family: 'Courier New'"> return "("+this.name+" , "+this.age+")";</span></p><p><span style="font-family: 'Courier New'"> }</span></p><p><span style="font-family: 'Courier New'">}</span></p></blockquote><p></p>
[QUOTE="koondeGoda, post: 2202755, member: 61636"] [B]Here is a code that demos java.util.Vector member methods. [/B] [B]To whom intersested in J.[/B] [B]__________________________________________________[/B] [FONT=Courier New]/* * a program demonstrating the operation of vector member functions */ import java.io.*; import java.util.Vector;[/FONT] [FONT=Courier New]class testVectors { // main public static void main(String a[]){ Vector v1 = new Vector(); processVector(v1); } //process the vector (format / display / manipulate) static void processVector(Vector v){ //fill the vector for ( int i=0; i < 5 ; i++) v.addElement(new Integer(i)); // [0,1,2,3,4] System.out.println(" v = "+v); Integer j = new Integer(3); // returns the occurences of 3 in vector v , and the occurences from index 4 System.out.println(v.indexOf(j)+" "+v.indexOf(j,4)); // returns whether v contains 3 , and last index of 3 System.out.println(v.contains(j)+" "+v.lastIndexOf(j)); [/FONT] [FONT=Courier New] // new vector v2 Vector v2 = new Vector(3,4); // capacity=3 capacity increment=4 // fill the vector v2 for(int i=4 ; i <8 ; i++) v2.addElement(new Integer(i)); // [4,5,6,7,8] // extend the size of the vector to accomodate at least specified objects. v2.ensureCapacity(9); Vector v3 = new Vector(2); // creates a vec capacity of 2 v3.setSize(4); // size = capacity = 4 v3 = [null,null,null,null] v3.setElementAt(new Integer(9), 1); // [null,9,null,null] v3.setElementAt(new Integer(3), 3); // [null,9,null,3] // insert a element at given position after shifting elements at positions // following the giben position by one. v3.insertElementAt(v3.elementAt(3),1); // [null ,3,9,null,3] v3.ensureCapacity(9); v3.removeElement(new Integer(9)); // [null,3,null,3] v3.removeElementAt( v3.size()- 2); // [null,3,3] System.out.println("v3 "+v3); //elements() returns an Enumeration obj that enumerates all the // objects in the vector java.util.Enumeration ev = v3.elements(); while(ev.hasMoreElements()) System.out.println(ev.nextElement()+ ""); System.out.println(); v3.removeElementAt(0); // [3,3] //add all collection v v3.addAll(v); // [3,3,0,1,2,3,4] // removes the contains of v2 v3.removeAll(v2); // [3,3,0,1,2,3] // adds the collection to a specified position v3.addAll(2,v) ; // [3, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3] // removes the objects which are not int the collection specified // in this case objects not in v2 will be removed from v3 v3.retainAll(v2); // [4] , intersection(v3,v2) v.subList(1,3).clear(); // [0,3,4] Vector v4 = new Vector() , v5 ; v4.addElement( new Node("Jill",23)); v5 = (Vector)v4.clone(); // v4 = [("Jill",23)] ((Node)v4.firstElement()).age = 34; // v4 = v5 = [("Jill",34)] System.out.println("v4 "+v4+" \n v5 "+v5); /******************************************************************/ /* The method clone() should be used carefully. */ /* It clones the array implementing the vector */ /* but not THE OBJECTS OF ARRAY . */ /* after method finished the cloned vector includes references to */ /* the same objects as the vector from which it was cloned. */ /******************************************************************/ } }[/FONT] [FONT=Courier New]class Node { String name; int age; Node(String s , int n){ this.name=s; this.age=n; } public String toString(){ return "("+this.name+" , "+this.age+")"; } }[/FONT] [/QUOTE]
Insert quotes…
Verification
Hata thunen beduwama keeyada? (60 bedeema thuna)
Post reply
Top
Bottom