By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

Using Vector in Java script

P: 79
Can we use Vector type of Java in Java Script
May 29 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
You can use SVG, canvas, VML, etc. or use a graphics library. A simple search should give some useful links.
May 29 '08 #2

P: 79
I want to use Vector class of java which is used for dynamic array
May 29 '08 #3

acoder
Expert Mod 15k+
P: 16,027
What do you want to use it for? How about using it in an applet and then using JavaScript to invoke it?
May 29 '08 #4

P: 79
What do you want to use it for? How about using it in an applet and then using JavaScript to invoke it?
I am using some API's. But the handler provided by tool is for writing API is in Java Script.The method in API has return type of vector.
May 29 '08 #5

acoder
Expert Mod 15k+
P: 16,027
Can you provide more details. I'm not quite following.
May 29 '08 #6

P: 79
Can you provide more details. I'm not quite following.
I got this vector class from some website for implementation of vector in java script
Expand|Select|Wrap|Line Numbers
  1. function Vector(inc) {
  2.     if (inc == 0) {
  3.         inc = 100;
  4.     }
  5.  
  6.     /* Properties */
  7.     this.data = new Array(inc);
  8.     this.increment = inc;
  9.     this.size = 0;
  10.  
  11.     /* Methods */
  12.     this.getCapacity = getCapacity;
  13.     this.getSize = getSize;
  14.     this.isEmpty = isEmpty;
  15.     this.getLastElement = getLastElement;
  16.     this.getFirstElement = getFirstElement;
  17.     this.getElementAt = getElementAt;
  18.     this.addElement = addElement;
  19.     this.insertElementAt = insertElementAt;
  20.     this.removeElementAt = removeElementAt;
  21.     this.removeAllElements = removeAllElements;
  22.     this.indexOf = indexOf;
  23.     this.contains = contains
  24.     this.resize = resize;
  25.     this.toString = toString;
  26.     this.sort = sort;
  27.     this.trimToSize = trimToSize;
  28.     this.clone = clone;
  29.     this.overwriteElementAt;
  30. }
  31.  
  32. // getCapacity() -- returns the number of elements the vector can hold
  33. function getCapacity() {
  34.     return this.data.length;
  35. }
  36.  
  37. // getSize() -- returns the current size of the vector
  38. function getSize() {
  39.     return this.size;
  40. }
  41.  
  42. // isEmpty() -- checks to see if the Vector has any elements
  43. function isEmpty() {
  44.     return this.getSize() == 0;
  45. }
  46.  
  47. // getLastElement() -- returns the last element
  48. function getLastElement() {
  49.     if (this.data[this.getSize() - 1] != null) {
  50.         return this.data[this.getSize() - 1];
  51.     }
  52. }
  53.  
  54. // getFirstElement() -- returns the first element
  55. function getFirstElement() {
  56.     if (this.data[0] != null) {
  57.         return this.data[0];
  58.     }
  59. }
  60.  
  61. // getElementAt() -- returns an element at a specified index
  62. function getElementAt(i) {
  63.     try {
  64.         return this.data[i];
  65.     } 
  66.     catch (e) {
  67.         return "Exception " + e + " occured when accessing " + i;    
  68.     }    
  69. }
  70.  
  71. // addElement() -- adds a element at the end of the Vector
  72. function addElement(obj) {
  73.     if(this.getSize() == this.data.length) {
  74.         this.resize();
  75.     }
  76.     this.data[this.size++] = obj;
  77. }
  78.  
  79. // insertElementAt() -- inserts an element at a given position
  80. function insertElementAt(obj, index) {
  81.     try {
  82.         if (this.size == this.capacity) {
  83.             this.resize();
  84.         }
  85.  
  86.         for (var i=this.getSize(); i > index; i--) {
  87.             this.data[i] = this.data[i-1];
  88.         }
  89.         this.data[index] = obj;
  90.         this.size++;
  91.     }
  92.     catch (e) {
  93.         return "Invalid index " + i;
  94.     }
  95. }
  96.  
  97. // removeElementAt() -- removes an element at a specific index
  98. function removeElementAt(index) {
  99.     try {
  100.         var element = this.data[index];
  101.  
  102.         for(var i=index; i<(this.getSize()-1); i++) {
  103.             this.data[i] = this.data[i+1];
  104.         }
  105.  
  106.         this.data[getSize()-1] = null;
  107.         this.size--;
  108.         return element;
  109.     }
  110.     catch(e) {
  111.         return "Invalid index " + index;
  112.     }
  113.  
  114. // removeAllElements() -- removes all elements in the Vector
  115. function removeAllElements() {
  116.     this.size = 0;
  117.  
  118.     for (var i=0; i<this.data.length; i++) {
  119.         this.data[i] = null;
  120.     }
  121. }
  122.  
  123. // indexOf() -- returns the index of a searched element
  124. function indexOf(obj) {
  125.     for (var i=0; i<this.getSize(); i++) {
  126.         if (this.data[i] == obj) {
  127.             return i;
  128.         }
  129.     }
  130.     return -1;
  131. }
  132.  
  133. // contains() -- returns true if the element is in the Vector, otherwise false
  134. function contains(obj) {
  135.     for (var i=0; i<this.getSize(); i++) {
  136.         if (this.data[i] == obj) {
  137.             return true;
  138.         }
  139.     }
  140.     return false;
  141. }
  142.  
  143. // resize() -- increases the size of the Vector
  144. function resize() {
  145.     newData = new Array(this.data.length + this.increment);
  146.  
  147.     for    (var i=0; i< this.data.length; i++) {
  148.         newData[i] = this.data[i];
  149.     }
  150.  
  151.     this.data = newData;
  152. }
  153.  
  154.  
  155. // trimToSize() -- trims the vector down to it's size
  156. function trimToSize() {
  157.     var temp = new Array(this.getSize());
  158.  
  159.     for (var i = 0; i < this.getSize(); i++) {
  160.         temp[i] = this.data[i];
  161.     }
  162.     this.size = temp.length - 1;
  163.     this.data = temp;
  164.  
  165. // sort() - sorts the collection based on a field name - f
  166. function sort(f) {
  167.     var i, j;
  168.     var currentValue;
  169.     var currentObj;
  170.     var compareObj;
  171.     var compareValue;
  172.  
  173.     for(i=1; i<this.getSize();i++) {
  174.         currentObj = this.data[i];
  175.         currentValue = currentObj[f];
  176.  
  177.         j= i-1;
  178.         compareObj = this.data[j];
  179.         compareValue = compareObj[f];
  180.  
  181.         while(j >=0 && compareValue > currentValue) {
  182.             this.data[j+1] = this.data[j];
  183.             j--;
  184.             if (j >=0) {
  185.                 compareObj = this.data[j];
  186.                 compareValue = compareObj[f];
  187.             }                
  188.         }    
  189.         this.data[j+1] = currentObj;
  190.     }
  191. }
  192.  
  193. // clone() -- copies the contents of a Vector to another Vector returning the new Vector.
  194. function clone() {
  195.     var newVector = new Vector(this.size);
  196.  
  197.     for (var i=0; i<this.size; i++) {
  198.         newVector.addElement(this.data[i]);
  199.     }
  200.  
  201.     return newVector;
  202. }
  203.  
  204. // toString() -- returns a string rep. of the Vector
  205. function toString() {
  206.     var str = "Vector Object properties:\n" +
  207.               "Increment: " + this.increment + "\n" +
  208.               "Size: " + this.size + "\n" +
  209.               "Elements:\n";
  210.  
  211.     for (var i=0; i<getSize(); i++) {
  212.         for (var prop in this.data[i]) {
  213.             var obj = this.data[i];
  214.             str += "\tObject." + prop + " = " + obj[prop] + "\n";
  215.         }
  216.     }
  217.     return str;    
  218. }
  219.  
  220. // overwriteElementAt() - overwrites the element with an object at the specific index.
  221. function overwriteElementAt(obj, index) {
  222.     this.data[index] = obj;
  223. }
  224.  
Now I have written this code .This code is written using API's
Expand|Select|Wrap|Line Numbers
  1. function getOptions(ctx)
  2.   {
  3.    var options=""; 
  4.  
  5.    var subroles = ctx.getAdminRole().getAttributeMultiValue("CUSTOM10");
  6.  
  7.    var v = new Vector(subroles);
  8.  
  9.    options = v.getElementAt(0);
  10.  
  11.  }
  12.  
  13.  
Now suppose the function getAttributeMultivalue is returning RoleA,RoleB
Now i want RoleA to be stored in getElementAt(0) and RoleB in getElementAt(1)
But with the above code i am getting RoleA,RoleB in getElementAt(0)
May 30 '08 #7

acoder
Expert Mod 15k+
P: 16,027
Oh, so that's what you mean by a vector! It can be easily confused with vector drawings.

From what I can see, the Vector constructor expects a number that determines the capacity of the vector. To add those roles to the vector either use addElement or insertElementAt.
May 30 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.