473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Using Vector in Java script

Can we use Vector type of Java in Java Script
May 29 '08 #1
7 2532
acoder
16,027 Expert Mod 8TB
You can use SVG, canvas, VML, etc. or use a graphics library. A simple search should give some useful links.
May 29 '08 #2
I want to use Vector class of java which is used for dynamic array
May 29 '08 #3
acoder
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
Can you provide more details. I'm not quite following.
May 29 '08 #6
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
16,027 Expert Mod 8TB
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

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

Similar topics

1
by: Scott Bates | last post by:
I am attempting to create a JAXB Object that returns the CollectionType as a List. In my schema file, I used the following: <jxb:globalBindings> collectionType="java.util.Vector"...
28
by: VK | last post by:
A while ago I wrote a "Vector data type" script using DOM interface to select.options. That was a (beautiful) mind game :-) rather than a practical thing to use. Here is another attempt open...
0
by: jlconde | last post by:
I have a classe to send mails. It runs on yahoo well but with hotmail I never receive the mails.I do not receive an error neither. I would need some strange header to make the hotmail like my...
0
by: doobybug | last post by:
Hi all, I really need your help! I am new to java and have some problems with my code. I have a program which inputs questionnaires and creates an object for each questionnaire. These objects are...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.