Vector notation - Java vs Delphi

Good morning everybody,
On this beautiful day I've the opportunity to learn java. I have to upgrade an existing application that makes use of a class 'TreeElement' with a (vector) field 'childs' .
public class TreeElement {
  private Vector childs;
  public Vector getChilds () { return childs; }
  public string label;
}In the vector childs instances of 'TreeElement' are stored. In that way a tree is formed, but that's not the question at hand.
I want to ask something about the java syntax. Is it necessary to write all this (especially the second part)?
Vector layer = null;
TreeElement element = null;
String label = null;
layer = tree.getChilds();
element = (TreeElement) Layer.get(0);
name = element.label;Firstly, a vector has to be defined to store the childs. Secondly a TreeElement has to be defined to store each child. Also a typecast seems to be necessary. And only now is it possible to access the method name.
In Delphi this would be
Name = tree.childs[0].label;And this can be written even shorter when making use of default properties:
Name = tree[0].label;Is something alike possible in java?name = tree.getChilds().get(0).labelWhy not?

With Java 1.5 you can write public class TreeElement {
  private List<TreeElement> children;
  public List<TreeElement> getChilds () { return children; }
  public String label;
}and name = tree.getChilds().get(0).label

Similar Messages

  • Using Vector in Java script

    I got this vector class from some website for implementation of vector in java script
    function Vector(inc) {
         if (inc == 0) {
              inc = 100;
         /* Properties */
         this.data = new Array(inc);
         this.increment = inc;
         this.size = 0;
         /* Methods */
         this.getCapacity = getCapacity;
         this.getSize = getSize;
         this.isEmpty = isEmpty;
         this.getLastElement = getLastElement;
         this.getFirstElement = getFirstElement;
         this.getElementAt = getElementAt;
         this.addElement = addElement;
         this.insertElementAt = insertElementAt;
         this.removeElementAt = removeElementAt;
         this.removeAllElements = removeAllElements;
         this.indexOf = indexOf;
         this.contains = contains
         this.resize = resize;
         this.toString = toString;
         this.sort = sort;
         this.trimToSize = trimToSize;
         this.clone = clone;
         this.overwriteElementAt;
    // getCapacity() -- returns the number of elements the vector can hold
    function getCapacity() {
         return this.data.length;
    // getSize() -- returns the current size of the vector
    function getSize() {
         return this.size;
    // isEmpty() -- checks to see if the Vector has any elements
    function isEmpty() {
         return this.getSize() == 0;
    // getLastElement() -- returns the last element
    function getLastElement() {
         if (this.data[this.getSize() - 1] != null) {
              return this.data[this.getSize() - 1];
    // getFirstElement() -- returns the first element
    function getFirstElement() {
         if (this.data[0] != null) {
              return this.data[0];
    // getElementAt() -- returns an element at a specified index
    function getElementAt(i) {
         try {
              return this.data;
         catch (e) {
              return "Exception " + e + " occured when accessing " + i;     
    // addElement() -- adds a element at the end of the Vector
    function addElement(obj) {
         if(this.getSize() == this.data.length) {
              this.resize();
         this.data[this.size++] = obj;
    // insertElementAt() -- inserts an element at a given position
    function insertElementAt(obj, index) {
         try {
              if (this.size == this.capacity) {
                   this.resize();
              for (var i=this.getSize(); i > index; i--) {
                   this.data[i] = this.data[i-1];
              this.data[index] = obj;
              this.size++;
         catch (e) {
              return "Invalid index " + i;
    // removeElementAt() -- removes an element at a specific index
    function removeElementAt(index) {
         try {
              var element = this.data[index];
              for(var i=index; i<(this.getSize()-1); i++) {
                   this.data[i] = this.data[i+1];
              this.data[getSize()-1] = null;
              this.size--;
              return element;
         catch(e) {
              return "Invalid index " + index;
    // removeAllElements() -- removes all elements in the Vector
    function removeAllElements() {
         this.size = 0;
         for (var i=0; i<this.data.length; i++) {
              this.data[i] = null;
    // indexOf() -- returns the index of a searched element
    function indexOf(obj) {
         for (var i=0; i<this.getSize(); i++) {
              if (this.data[i] == obj) {
                   return i;
         return -1;
    // contains() -- returns true if the element is in the Vector, otherwise false
    function contains(obj) {
         for (var i=0; i<this.getSize(); i++) {
              if (this.data[i] == obj) {
                   return true;
         return false;
    // resize() -- increases the size of the Vector
    function resize() {
         newData = new Array(this.data.length + this.increment);
         for     (var i=0; i< this.data.length; i++) {
              newData[i] = this.data[i];
         this.data = newData;
    // trimToSize() -- trims the vector down to it's size
    function trimToSize() {
         var temp = new Array(this.getSize());
         for (var i = 0; i < this.getSize(); i++) {
              temp[i] = this.data[i];
         this.size = temp.length - 1;
         this.data = temp;
    // sort() - sorts the collection based on a field name - f
    function sort(f) {
         var i, j;
         var currentValue;
         var currentObj;
         var compareObj;
         var compareValue;
         for(i=1; i<this.getSize();i++) {
              currentObj = this.data[i];
              currentValue = currentObj[f];
              j= i-1;
              compareObj = this.data[j];
              compareValue = compareObj[f];
              while(j >=0 && compareValue > currentValue) {
                   this.data[j+1] = this.data[j];
                   j--;
                   if (j >=0) {
                        compareObj = this.data[j];
                        compareValue = compareObj[f];
              this.data[j+1] = currentObj;
    // clone() -- copies the contents of a Vector to another Vector returning the new Vector.
    function clone() {
         var newVector = new Vector(this.size);
         for (var i=0; i<this.size; i++) {
              newVector.addElement(this.data[i]);
         return newVector;
    // toString() -- returns a string rep. of the Vector
    function toString() {
         var str = "Vector Object properties:\n" +
         "Increment: " + this.increment + "\n" +
         "Size: " + this.size + "\n" +
         "Elements:\n";
         for (var i=0; i<getSize(); i++) {
              for (var prop in this.data[i]) {
                   var obj = this.data[i];
                   str += "\tObject." + prop + " = " + obj[prop] + "\n";
         return str;     
    // overwriteElementAt() - overwrites the element with an object at the specific index.
    function overwriteElementAt(obj, index) {
         this.data[index] = obj;
    Now I have written this code .This code is written using API'sfunction getOptions(ctx)
    var options="";
    var subroles = ctx.getAdminRole().getAttributeMultiValue("CUSTOM10");
    var v = new Vector(subroles);
    options = v.getElementAt(0);
    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)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    abhishek20 wrote:
    well the question is also about Vector class of JavaNo it is not you doofus. It is about JavaScript. Your question has fuck all to do with Java.
    so I thought to post it here.That was a mistake.
    any ways if you don't know java script or anything about vector class then you should not reply.I know about JavaScript.
    I know about Vectors.
    I will not tell you anything about JavaScript here.
    I wouldn't tell you anything useful anyway because I have taken a personal dislike to you.
    >
    No need to make me understand what is the purpose of this forum.Really? Because you don't seem to get it. At all.
    next time take care before you reply.Next time engage your tiny pea brain in some sort of logical thought before posting.

  • Calling Java from Delphi

    Can ActiveX bridge used for calling Java from Delphi?

    Also trying to solve this problem, after creating the bean i packaged/registered it successfully, (tested the bean using beanbox, works perfectly) then tried calling it via createoleobject in delphi 7
    (*Code Snip *)
    Var
    v : variant;
    begin
    try
    v := createoleobject(Edit1.text); // my class name ie WorldPort.Bean (also tried WorldPort.Bean.1
    except on e:exception do
    showmessage(e.Message)
    end;
    end;
    (*Code Snip*)
    After this simple code failed i started backtracking , eventually after getting "Access Violation...etc in axbridge.dll" all the time. tried delphi "import activex control" got the same access violation.
    Decided to test the example from http://java.sun.com/j2se/1.4.2/docs/guide/beans/axbridge/developerguide/examples.html........ they both crashed on execution (calc.exe and boundprop.exe).
    After this failed I tested these example classes on another machine, both crashed again.....
    Also tried this experiment in VC++ , after adding the class to the Toolbox , i try to drop it onto a form, once again access violation.**PS**
    ('You will need VC++ and the Microsoft SDK to build the dll, download c++beta for free from microsoft site it you need it')** and a simple cmd file todo the job would look something like this:
    echo - ** Build Java ActiveX Bridge **
    echo Make sure these folders exist in jre--> axbridge\lib and axbridge\bin
    cd\SDK\jre\axbridge\bin
    set path=%path%;C:\MSDN\Bin;C:\VisualC++\lib
    echo path
    javac WorldPort.java
    jar cmf Manifest.txt WorldPort.jar WorldPort.class
    "C:\SDK\bin\packager.exe" -clsid {162193C4-AD5C-4A06-9F88-A737AE9B43AD} -out "C:\SDK\jre\axbridge\bin" WorldPort.jar WorldPort-reg
    *** After running this cmd you will find the dll in bin folder and the jar in lib folder..***
    Must Read:http://java.sun.com/j2se/1.4.2/docs/guide/beans/axbridge/developerguide/index.html
    Hope some of this helps , if you have successfully created / installed/ used a "JavaActiveX" in Delphi , i would really like to try replicate it on my machine..

  • Using a vector in Java IDL...URGENT!

    How can I use a vector in Java IDL? I am trying to have a function return a vector.
    From what I have read, I should use a sequence. Therefore I tried the following code:
         struct ItemDB {
              long ItemNumber;
              long Quantity;
              double Price;
    typedef sequence <ItemDB> ItemList;
         interface ProductOperations{          
              ItemList listItems();
    When the equivalent Java is generated for the interface I am forced to return an array instead. This is what is generated:
    public interface ProductOperationsOperations
    ItemDB[] listProducts ();
    However, this is an array, which is bounded. I want to use an unbounded storage device.
    Can someone please please assist me. I appreciate it.
    Thanks
    RG

    This is already an unbounded sequence
    typedef sequence <ItemDB> ItemList;
    Checkout the read/write methods in the Helper class and you can see that the above list is unbounded.

  • Calling Java from Delphi via JNI

    Hi all. I've got a J2EE application, and I'm trying to write a Delphi client for the server. More specifics:
    Server and test client running in Win2000 SP4.
    Server running in JBoss (JUnit test cases have successfully connected to the server)
    JDK 1.4
    I'm using some Delphi files from JEDI which allow me to load/access a JVM via JNI. So far, I've been able to:
    1) Create a Properties object.
    2) Populate the Properties object with String values (making sure to use NewStringUTF to pass into the Properties method
    3) Find my java client classes which facilitate opening a connection.
    However, when I attempt to call the method on the object which actually creates the connection, I get an Exception.
    My immediate question is how do I see what the Exception is? I have an Exception object, but ExceptionDescribe doesn't product anything, and I'm having trouble finding any details about the Exception (what type of exception, what the Message or Call Stack is). At the moment, something's wrong but I can't see what. And I'll have no chance of fixing it if I don't know what the problem is.
    Thanks for any help,
    Ed

    I use some code for solving this task (in real project with Delphi 6.0 & Java 1.4.1).
    May be, it will help.
    procedure TJavaInterp.CheckJNIException(Message : string);
    begin
    if JNI_ExceptionCheck(JNIEnv) = JNI_TRUE then
    begin
    JNI_ExceptionDescribe(JNIEnv);
    JNI_ExceptionClear(JNIEnv);
    raise Exception.Create(Message);
    end;
    end;{ TJavaInterp.CheckJNIException }
    procedure TJavaInterp.HandleException(excpt : jthrowable);
    var
    Msg: string;
    ESyntax : Exception;
    CauseName : WideString;
    Tag : OleVariant;
    begin
    if JNI_IsInstanceOf(JNIEnv, excpt, FclsCommonSyntaxError) = JNI_TRUE then
    begin
    ESyntax := Self.BuildSyntaxException(excpt);
    JNI_DeleteLocalRef(JNIEnv, excpt);
    raise ESyntax;
    end;
    Msg := Self.GetMessage(excpt);
    if JNI_IsInstanceOf(JNIEnv, excpt, FclsNPScriptRuntimeException) = JNI_TRUE then
    begin
    CauseName := Self.GetCauseName(excpt);
    Tag := Self.GetTag(excpt);
    JNI_DeleteLocalRef(JNIEnv, excpt);
    raise NPScriptHelper.BuildNPScriptRuntimeException(Msg, CauseName, Tag);
    end;
    if JNI_IsInstanceOf(JNIEnv, excpt, FclsHaltException) = JNI_TRUE then
    begin
    JNI_DeleteLocalRef(JNIEnv, excpt);
    raise Exception.Create(Msg);
    end;
    Msg := Self.ToString(excpt);
    JNI_DeleteLocalRef(JNIEnv, excpt);
    raise Exception.Create(Msg);
    end;{ TJavaInterp.HandleException }

  • How do I count objects of a specific type in a Vector? Java 1.3 source.

    How do I count the objects of a specific type in a Vector?

    isInstance may allow too much for your needs as it allows any object which can be typecast to 'type' while you seem to want to limit to objects which are exactly of the type 'type'. In such a case you could do this:
    for (Enumeration e = attachmentTypes.elements(); e.hasMoreElements();)
      Object check = e.nextElement();
      if (check.getClass().equals(type)) {
        counter++;
    }Javadoc of the isInstance() method:
    http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#isInstance%28java.lang.Object%29
    (I link to the old Javadoc given you mentioned Java 1.3 compatible source)

  • Vector in java

    hi all
    i m having one vector and i need all elements of that vector except first element. can anyone help me?
    my vector is like
    [xyz,pqr,abc,mno]
    out of this vector i don't want vector element ' xyz'.
    how to do?

    import java.util.*;
    class test
         public static void main(String args[])
              Vector v=new Vector();
              //storing the element          
              v.addElement("xyz");
              v.addElement("pqr");
              v.addElement("abc");
              v.addElement("mno");
              //removing the element
              v.removeElementAt(0);
              Enumeration venum=v.elements();
              //printing the document
              while(venum.hasMoreElements())
                   System.out.print(venum.nextElement()+" ");
              System.out.println();
    }

  • Vector in java 5

    How to use the new vector<E> class. We need to add all java types in vector.

    you dont have to put that there but you can makes life easier as then you dont need to cast values that are pased back from things like vector.elementAt(0);
    David

  • Getting Data from C++ STL Vector into Java Vector

    Hi All,
    I have been involved in a project recently to port a lot of C++ code into Java. Testing the ported Java code has been done in isolation with JUnit and works fine, but we would like to test all the ported code. To do this we aim to call the ported (Java) methods from C++ using JNI.
    I have read a lot of the documentation from Sun on the JNI and can access and set Java methods that only contain primitive types without any problems. However what I cant seem to be able to achieve is passing aggregated C++ classes to Java.
    Java
    public void translation_unit_2( Vector v ) { \* code *\ }and somewhere in C++
    jobject actions;                  // Is initialized
    std::vector<Node> test ;   // Node class may contain other objects
    jmethodID tmp = env->GetMethodID(env->GetObjectClass(actions), "translation_unit_2", "(Ljava/util/Vector;)V");
    env->CallObjectMethod(actions, tmp, test);Now obviously what I have posted there doesnt work, but can anybody point me
    in the correct direction to pass the Vector and its Data in C++ to Java?
    None of the Java methods are native, because the eventually the whole system
    will be operating solely in Java, but the testing at this stage is quite important.
    If this is an impossible task then please say so also, any help will be appreciated !
    Thanks,
    Mark Hennessy

    Thanks for the reply and I understand that running an iterator
    over the vector would be the way the get the data out of the C++ Vector.
    However that still leaves me with the question of mapping an arbitrary
    C++ class ( which in the case of the example above would be a Node class which is
    composed of other aggregated classes ) to a jobject such that I can pass
    the data easily to Java throught JNI?
    Is this possible or would I have to decompose each and every class to its most
    primitive types and pass them over to Java and reconstruct them on the Java side?

  • Vectors in java

    I have created a vector inside vector
    Now my vector contains [one,two ,three]
    I want to display the contents of this vector in fashion
    two
    one
    three
    How can I do that?

    I use vectors bcos each time i run my application it has to store randomly generated data, whose length i cant pre-determine.ArrayList will adjust the length for you. Just use java.util.ArrayList instead of java.util.Vector. It will do the same thing, only faster (if you need the synchronization that Vector provides, there are methods in java.util.Collections to help you with that).
    May I now ask another question, I am looking for random number distribution generator in java. i could
    write one, but i figured i will just reuse. this will be different from the Math.random or rand() in java.
    I need it for a random exponential distribution.If nothing in java.util.Random works, then you could write your own, possibly using the output of those methods as a starting point. Otherwise, there are probably pre-existing Java exponential distribution classes on the web if you search for them.

  • What are vectors in java ?

    Dear forum users ,
    I would like to know if there be any resources available or any information available on what are vectors and how do they function in java . thank you .

    Dear forum users thanks again for your replies ,
    basically im trying to understand this below code which im not really sure whether its a vector or a for loop and if its neither what does it mean ,could anyone explain the part i have indicated below in bold especially the part on the for(int i=msgs.size()-1; i>=msgs.size()-maxText,i--) and the msg=(Message)msgs.elementAt( i ) ; and why the need for a declaration of a integer j,p=-1
    int maxText = 11; //max texts displayed
    if (history || msgs.size() < maxText)
    maxText = msgs.size();//all texts
    history = false; //reset
    int lin = 0; //links count
    *for (int i=msgs.size()-1; i>=msgs.size()-maxText; i- -) {*
    msg = (Message) msgs.elementAt( i );
    int j,p = -1;

  • Passing vector between java class and jsp page

    Hi,
    I think this is similar to the last post but anyways maybe somebody can help us both out bontyh.....I am getting an error saying undefined variable or class when i try to access a Vector which was returned in a class.
    Anyways anyone got any ideas?

    Maybe if you give some more information...

  • Retuning a vector in java

    I want to return a vector from a method how do i do it. Please explain me with a suitable
    example. I'll be very gratefull.
    Its very urgent

    What's the input? if it's a string try this:
    public Vector tokens(String original,String divider) {
        Vector v = new Vector();
        StringTokenizer st = new StringTokenizer(original,divider);
        while(st.hasMoreTokens()) v.add(st.nextToken());
        return v;
    }Shish

  • Help needed to solve "java.io.NotSerializableException: java.util.Vector$1"

    hi to all,
    i am using a session less bean A to querry a Entity Bean B , which inturns calls another EntityBean C ,
    finally a ' find' method is invoked on the EntityBean C, In this a vector is created which holds 3 different vectors at different indexes, now the problem i am facing is that when Enity Bean B is returning the final vector to the A , it's firing out a errors that :-
    TRANSACTION COULD NOT BE COMPLETED: RemoteException occurred in server thread;
    ested exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.rmi.ServerException: RemoteException occurred in server thread; ne
    ted exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.ServerException: RemoteException occurred in server thread; nested exc
    ption is:
    java.rmi.RemoteException: null; nested exception is:
    java.rmi.ServerException: RemoteException occurred in server thread; ne
    ted exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.RemoteException: null; nested exception is:
    java.rmi.ServerException: RemoteException occurred in server thread; ne
    ted exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.ServerException: RemoteException occurred in server thread; nested exc
    ption is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.io.NotSerializableException: java.util.Vector$1
    <<no stack trace available>>
    ur any help would be highly appricated to solve out this prob.
    If i try to iterate through this vector it's gives IOR:0232003x343242344asdsd................................................blabla....................
    thanxs in adavance
    Deepak

    Hi I think you are using the method elements() in a remote method.
    This method can't be Serializable!! Because it returns an Interface. Interfaces are never Serializable.
    Regards,
    Peter

  • Converting Java Vector to JavaScript Array

    Hi ,
    If any body know , plz let me Know ...
    I want to Convert Java Vector of Vectors to Java Script Array of Arrays ..Thanks in Advace ..
    SUBBA.

    Hi,
    I did something like this.I used hidden variables to pass all the info the client side and I called "onload" on the body tag.In this onload function call , i split that hidden variable into array of arrays.
    This needs to be done using proper delimiters.
    So the hidden variable wuold be of the form:
    V1E1#V1E2#V1E3#V1E4$$V2E1#V2E2#V2E3#$$
    V1E1 means Vector 1 Element 1.
    You need to form this properly on server and pass it to the client where it would be split onLoad and stores appropriately into arrays.
    thx.

Maybe you are looking for