Dynamic array declaration

Hi
This is a simple one but I need the corect way to declare
arrays in a for loop. I need the correct syntax for creating 10
arrays for example named dp0 ..dp9.
Thanks for your help.
for (i=0;i<10;i++){
var dp'i' Array= new Array; //This does not work,
}

hello, this kind of syntax or something like this was never
allowed
var "dp"+i : Array
var dp[ i ] : Array
you can't create variables with dynamique name like this,
the trick is to full an array (or an Object) with dynamical index
(or propertie) :
var dp : Array = new Array()
var dp2 : Object = {}
for (var i : int = 0 ; i<10 ; i++)
dp[ i ] = new Array(i,i*i);
dp2[ i ]= new Array(i,i*i);
//notice that you dont use 'var' and typing (' : Array')
cause you only add dynamicly index or property you dont create var
}

Similar Messages

  • Deleting dynamic arrays

    My main concern is worrying about having a memory leak due to a program crashing before it has the chance to reach the line of code in which technically releases its memory. What I do not understand is, if the program does crashes before it can release the memory of any dynamic arrays, wouldn't the memory leak still exist the next time you run the program and causing the leak to get worse each time?
    for example, if I had something like this...
    int main()
    int *a = new int[10];
    bool game = true;
    // then for some reason the program crashes somewhere along these lines...
    while(game) {
    do some code in which some reason cause the program to crash
    delete [] a;
    return 0;
    here in the example, if the program had crashed inside the while loop, wouldn't I have a memory leak which exist forever?

    BnTheMan wrote:
    The first instance is I wanted to know after reading some topics on the internet, I have heard that is is not a good Idea to set a pointer to know inside of a constructer class like this...
    should you do this, or does it depend on the compiler that you use, and what about if you are using XCode?
    Did you mean to type "not a good Idea to set a pointer to NULL"? That is what you code seems to say.
    They key part is memory management. People have been screwing that up for decades. You should initialize pointers to NULL or 0 (same thing) before using them. Then, when you are done with them, if they aren't NULL, you must release them. Even if you haven't allocated a pointer, you can always release it if it is NULL using free(), delete, or release - depending on what language you are using.
    If you allocate a pointer, then release its memory, you should re-set it back to NULL immediately if there is any chance that some other bit of code could try to release the memory. If you try to free a pointer twice, your app will crash.
    All this is true in any environment that doesn't have garbage collection. Don't think that garbage collection is the panacea it is made out to be. It is better to be able to manage your own memory when you need to. Plus, there are more system resources than just memory.
    2. The next thing I noticed was that it did not have any memory releasing code for when there is a problem with reading the file, such as if the file could not be opened. So I am wondering if something was to happen when opening the second MD2 file, in that it could not open the file because it was currupted, and I did something like this...
    MD2_Object::loadObject() {
    ifstream inFile;
    char buffer[64];
    inFile.open(myFile, istream::binary);
    if(!inFile) {
    ... the code I need to my question
    How can I tell the program that there was an error and that it needs to release the memory from the first dynamic array variable?
    (I added some { code } statements to format your code nicely. Reply to this post to see how I did it.)
    That is a good question, with several caveats. In your example, you are actually using static variables, not dynamic. All of your data is allocated on the stack. You didn't use any sort of malloc(), new, alloc, etc. Therefore, memory management is easy. When the current scope ends (usually the code between braces) anything allocated on the stack automatically goes away cleanly. Plus, since the stack is much more low level than dynamic memory, you can do stack allocations much more quickly.
    But, had used dynamic allocation, this would be an issue. It is a significant problem that is rarely handled correctly. If there is an error, you need to release any resources (such as memory) that you have allocated. There are a number of ways to handle this, depending on your language and how you want to do it.
    The first solution is to exploit the fact that, in C++, you can declare your variables anywhere you want. They don't have to be at the beginning of the scope. The best thing to do is don't allocate resources unless you know you can use them. If you fail halfway through, you would have to release all resources that you had allocated up to that point.
    Some other ideas are to use exceptions and wrap allocations in classes such as auto_ptr. Then, when you encounter an error, you throw and exception and you use the stack scoping rules, though static wrapper variables, to release dynamic data underneath.
    Yeah, it gets complicated. Maybe most more code that precisely shows what you are concerned with. Wrap your code between two lines of
    { code }
    { code }
    without the spaces and it will print out nicely. So far, it looks like you are doing everything correctly. Sometimes, instead of asking how to do something, you'll get better responses by just posting how you do it and, if there are any problems, people will pick it apart Harsh, but effective.

  • Populating a dynamic array with objects and managing it in runtime.

    So I'm another stuck firstyear. I'll try and make my question compact. I'm using Flash CS6 and have drawn an animated character on the stage that consists of separate parts that are animated and its head is a separate class/symbol entirely because it has not only animation, but a state switch timeline as well. This said Head extends the Main that is the character MovieClip.
    I am using a dynamic array to store and .push and .splice objects of another class that would collide with this said Head.
    I also discovered the super() function that is implicitly called as the constructor of the parent in any child class that extends the parent, in this case Head extends Main. The issue is that my collidable object array is populated within the main, within the function that spawns every next collidable object with a TimerEvent. This said function then gets called twice due to the super() call.
    I have tried putting this super() call into an impossible statement in my child class, but it doesn't change a thing, and it was said that this method is unsafe so I don't even know if it should be working.
    However what confuses me the most is when I trace() the .length of my collidable object array at the end of that function. As I said earlier, the original function both spawns an object after a period of Timer(1000) and adds it to the stage as well as adds the object onto the object array, but the super() constructor only duplicates the length call and a creates a copy of the object on the stage, but it does not add the second copy of the object onto the array. The trace() output goes on like so:
    1
    1
    2
    2
    3
    3
    4
    4
    etc.
    I wonder why and I'm really stumped by this.
    Here is the code in question:
    public class Main extends MovieClip {
                        public var nicesnowflake: fallingsnow;
                        var nicesnowflakespawntimer: Timer = new Timer(1000);
                        public var nicesnowflakearray: Array = new Array();
                        public function Main() {
                                  nicesnowflakespawntimer.addEventListener(TimerEvent.TIMER, nicesnowflakespawn);
                                  nicesnowflakespawntimer.start();
                        public function nicesnowflakespawn(event:TimerEvent) : void {
                                  nicesnowflake = new fallingsnow;
                                  nicesnowflake.x = Math.random()* stage.stageWidth;
                                  nicesnowflake.y = - stage.stageHeight + 100;
                                  nicesnowflakearray.push(nicesnowflake);
                                  stage.addChild(nicesnowflake);
                                  trace(nicesnowflakearray.length);
                                  for (var i:Number = 0; i < nicesnowflakearray.length; i++){
                                            nicesnowflakearray[i].addEventListener(Event.ENTER_FRAME, snowhit);
                        public function snowhit(event:Event) : void {
                                  if (nicesnowflakearray[0].y >= 460){
                                            if (nicesnowflakearray[0].y == stage.stageHeight) {
                                            nicesnowflakearray.splice(nicesnowflakearray.indexOf(nicesnowflake), 1);
                                  //if (this.hitTestObject(nicesnowflake)){
                                            //trace("hit");
    I am also fiddling with the collision, but I believe that it would sort itself out when I deal with the array pop and depop properly. However I'm pasting it anyway in case the issue is subtly hidden somewhere I'm not looking for it. And here is the child class:
    public class Head extends Main {
                        public function Head(){
                                  if (false){
                                            super();
                                  this.stop();
    So like what happens at the moment is that the array gets populated by the first object that spawns, but there is two objects on the stage, then when the objects reach stage.460y mark the array splices() the one object away and displays an error:
    "#1010: A term is undefined and has no properties.
              at Main/snowhit()"
    then when the next object spawns, it repeats the process. Why does it trace the array.length as "1, 1, 2, 2, 3, 3, 4, 4, 5, 5, etc" until the despawn point and then goes on to display an error and then starts from 1 again, because if the array length is more than one object at the time when the first object of the array gets spliced away, shouldn't it go on as usual, since there are other objects in the array?
    Thank you very much to whomever will read this through.

    There are multiple problems:
    1. You should add eventlisteners for your objects only once, but you add eventlisteners every time your timer runs to all of your snowflakes, again and again:
                                  for (var i:Number = 0; i < nicesnowflakearray.length; i++){
                                            nicesnowflakearray[i].addEventListener(Event.ENTER_FRAME, snowhit);
    change it to
    nicesnowflake.addEventListener(Event.ENTER_FRAME, snowhit);
    I don`t see why its even necessary to employ this snowflakearray, it would be much straight forward if you simply let the snowflakes take care of themselves.
    2. Then you have to change your enterframe function accordingly
    public function snowhit(event:Event) : void {
                                  if (e.currentTarget.y >= 460){
                                            if (e.currentTarget.y == stage.stageHeight) {
                                            e.currentTarget.removeEventlistener(Event.ENTER_FRAME, snowhit);
                                            removeChild(e.currentTarget);
    3.
                                  //if (this.hitTestObject(nicesnowflake)){
                                            //trace("hit");
    since "this" is a reference to the Main class (root) it surely won`t function as you intend it to.
                                  if (false){
                                            super();
    makes no sense to use a condition that can never be true

  • String Array declaration with multiple lines

    I have an array declaration with several indexes where it covers multiple lines.
    Please advise if this is the best way to declare an array declaration with multiple lines:
    String cityString = "San Diego, Oakland, London, New York, Dublin," +
    "Dallas, Milan";
    String city[] = cityString.split(",");

    String[] city = {"San Diego", "Oakland", "London",
    "New York", "Dublin", "Dallas", "Toronto"};You can use indentation and line breaks as you like, as usual in Java.

  • How to pass Stages Guid dynamically in declarative workflow

    hello,
    How to pass Stages Guid dynamically in declarative workflow.
    Example For List we used to pass guid like this 
    System.Guid.Parse("{$ListId:Lists/Testlist;}")
    help me out to pass stage guid...
    any help would be appreciated!!!!
    Thanks regards, Vignesh.

    You need to make use of command line arguments. You can go through this link for example.
    http://www.freeos.com/guides/lsst/ch02sec14.html
    In your shell script(say test.sh) you can have
    exp fas/xxxxxx@fasbd001 file=audit_grn_28aug.dmp tables=audit_grn query=\"where org_grp_i=$1 \"$1 indicates it accepts the first argument from command line. if you want to pass 19, add that next to the script you're executing.
    From command line you can call this shell script say test.sh 19

  • Dynamic array

    how to implement dynamic array size in java...??
    say if i want to add the element in an array then how should i do it..??

    For dynamic array...Vector is the best one i
    est one i think....
    Sigh. Unless other code requires it (for example, JTable's DefaultTableModel),
    Arraylist is preferred over the legacy class Vector.
    And even then I wouldn't recommend ArrayList as the "best" list. What if the OP
    wanted to have a FIFO queue? Would you recommend ArrayList/Vector over
    LinkedList?

  • Dynamic data declaration refering to local data defined in another program

    Here is the situation:
    The standard program SAPICP__ , SAPICF__ and several other of same series have a form routine REPOSITORY_DEVCLASS_ADD. this subroutine in all SAPI* programs has parameter REPOSITORY of type REPOSITORY (declared as internal table with header lines) and type REPOSITORY is declared with different component in each program.
    Now the problem is that i need to call subroutine REPOSITORY_DEVCLASS_ADD dynamically from my program and so i want to dynamically declare the table REPOSITORY in my program to pass to this subroutine and as program is not yet loaded into memory i have no way to achieve this.
    Can some one help in achieve this?
    Edited by: Gaurav B. on Jan 30, 2012 4:02 PM

    Hello Fred,
    That's a big task, even if it looks "easy". My first idea is, your request can not be covered by dynamic programming. a) every node has a different structure (I guess), b) the code itself has to be adjusted to the nodes in use as well. I.e. when a dynamic data declaration would be possible, the coding has to adhere the used number of nodes.
    My recommendation:
    - declare as many standard tables (nodes) as possible
    - the code for  each node has to be implemented
    - define the number of used nodes by a parameter or an interface
    Have success,
    Heinz

  • Creating a dynamic Array

    Thanks for reply.
    I want to create a dynamic array and for this I used ArrayList Collection and Vector Class. I am using JDK 1.4.1 . But , I have a problem in that. In ArrayList, I used the following code:
    ArrayList al = new ArrayList();
    while(rs.next())
    al.add(rs.getString("Enter_Year"));
    al.add(rs.getString("UG_Production_Mi_Te"));
    al.add(rs.getString("OC_Production_Mi_Te"));
    al.add(rs.getString("Total_Production_Mi_Te"));
    al.add(rs.getString("OBR_MM3"));
    al.add(rs.getString("OMS_Te"));
    al.add(rs.getString("Coal_Offtake_Mi_Te"));
    al.add(rs.getString("Supply_to_Steel_Plant_Th_Te"));
    al.add(rs.getString("Royalty_SalesTax_MP_Crs"));
    al.add(rs.getString("Royalty_SalesTax_CG_Crs"));
    al.add(rs.getString("Total_ManPower"));
    al.add(rs.getString("Fatality_Rate_per_Mi_Te_Output"));
    al.add(rs.getString("Total_Wellfare_Expenditure_Crs"));
    String rpt[][] = new String[al.size()][];
    rpt = al.toArray(rpt); // ArrayStoreExecption error
    String colheads[] = {"...........My Table's Column names are here....."};
    JTable table = new JTable(rpt,colheads);
    JScrollPane jsp = new JScrollPane(table);
    add(jsp);
    At the statement having comment line I received the error ArrayStoreException. The JTable accepts the 2 D array for storing the info and I think the toArray() method is not working there and can't convert the ArrayList elements into an Array. Similarily, Vector Class is also not converting the elements into an array object and showing the same error only.
    Please help me to convert it into an array object so that I can pass it into the JTable argument.

    That isn't a jdbc problem but rather a collection problem.
    You certainly can't convert a one dimensional collection into a two dimensional array automatically. And that is what you are creating with the while loop.
    Since you know the column names you also know the inner array size so you can start by creating a statically sized string array (not collection) in the loop and add that to the outer collection. That might solve the problem.

  • Array declaration problems

    hi
    i am having problems with my array declarations
    import java.io.*;
    public class bubbleSort4{
      public static void main (String []args) throws IOException{
        BufferedReader input=new BufferedReader (new FileReader("numbers.txt"));
        for (int blah=0; blah<0; blah+=1 ){
        int array1[];
        array1[blah]=Integer.parseInt(input.readLine());
        }//end of for
        input.close();
        bubbleSort4(array1);
      }//end of main
    public static void bubbleSort4(int[] array1) {
      int newLowest = 0;            // index of first comparison
        int newHighest = array1.length-1;  // index of last comparison
        while (newLowest < newHighest) {
            int highest = newHighest;
            int lowest  = newLowest;
            newLowest = array1.length;    // start higher than any legal index
            for (int i=lowest; i<highest; i++) {
                if (array1[i] > array1[i+1]) {
                   // exchange elements
                   int temp = array1; array1[i] = array1[i+1]; array1[i+1] = temp;
    if (i<newLowest) {
    newLowest = i-1;
    if (newLowest < 0) {
    newLowest = 0;
    }//end of if
    } else if (i>newHighest) {
    newHighest = i+1;
    }//end of else if
    }//end of if
    }//end of for
    }//end of while
    }//end method bubbleSort4
    }//end of class
    i get the error where i call the bubbleSort4 method with the array1 in brakets, it says that it doesnt recognize the variable. i know thats because i declared it in the for loop, but when i declare the array outside the for loop, it says that it was not initialized when i use it in the for loop. a vicious cycle. lol. so please, someone help....

    int array1[];you must initilize the array1
    example :
    int [] array1 = null;or
    int [] array1 = new int [10];
    for (int blah=0; blah<0; blah+=1 ){the codes will never go into the above for loop since
    blah always >= 0;

  • How to plot XY graph from 2 input of dynamic array ? ...

    I have 2 problem when I plot XY graph with 2 dynamic array :
    - I want to make the graph look like sweep chart. But it's seem not possible to use waveform graph?
    - The graph shoul move from left to right , then right to left, then left to right,.....
    Any one can give me some hints?  Thanks alot.
    Attachments:
    U1.PNG ‏21 KB
    XY radom value input.vi ‏147 KB

    I have to make Y change  from 0-10, then 10-0,.... Any one can help me to make it better?
    - How I shift the graph?
    Attachments:
    working_increse_decrease.vi ‏20 KB

  • Creating dynamic array

    Can anybody tell me how to create a dynamic array in Java??I dont want to fix size of array,it should take any number of values at runtime.Please tell me the syntax of using dynamic arrays.

    Hi,
    For creating an array of similar data elements, use
    ArrayList., but for storing elements which are in the
    form of object then use Vector...
    Best of Luck..
    R SWrong... the main different between ArrayList and Vector is that Vector was sychronized and ArrayList are not.

  • Dynamic interface declaration

    Hello,
    Is it possible to have a dynamic interface declaration like you can have a dynamic object declaration?
    For an object:
    data: lo_object type ref to object.
    create object lo_object type (lv_object_name).
    Is this in some way possible to do with an interface? I need to do this for an interface at runtime.
    Gr,
    Jaron

    You can create data of a dynamically given reference  type (which may be an interface) and move the object reference to it.
    Something like so:
    DATA:
          my_obj TYPE REF TO object,
          lr_data TYPE REF TO data.
      FIELD-SYMBOLS: <fs> TYPE ANY.
      CREATE OBJECT my_obj TYPE my_object_type.
    TRY.
      CREATE DATA lr_data TYPE REF TO (some_interface_type_name).
      ASSIGN lr_data->* TO <fs>.
       <fs> ?= my_obj.
      CATCH cx_sy_move_cast_error.
      CATCH cx_sy_create_data_error.
    ENDTRY.
    Basically, the assignment tests if my_obj is an instance of the type named some_interface_type_name.
    Hope this helps,
    Sebastian

  • How can i maintain a dynamic array

    I want to maintain an dynamic array in my code . It shoud grow accroding to no of user inputs . 1 can entre 5 numbers and other 10 , then array size should grow orderly 5 and 10 like that . Array is int array.

    Read up on the Collections API. There is a tutorial on this site. At first blush, you want a List. If it will grow and you will not be removing and inserting constantly from the 'middle' of the list of items, then ArrayList is a logical choice as your List. Otherwise, use LinkedList. But definitely bone up on the tutorial. Understanding how Collections work is fundamental to any Java programming you will do.
    - Saish

  • Creating a dynamic array in Actionscript

    I am wondering how I would create a dynamic array in Acionscript, i.e. one that utilizes fields for mapping data like this:
    public var SMITH:Array = [
               {date:"29-Aug-05", close:45.87, profit:52},
               {date:"23-Aug-05", close:45.74, profit:32},
               {date:"21-Aug-05", close:45.77, profit:22},
               {date:"25-Aug-05", close:46.06, profit:51},
    but where you would just be reading in data without knowing what your data looks like.  In other words, I'd like to take something like a database table with X number of columns and have Actionsctipt map each row to an item in my array.  Something like this:
    for each row in table
    add {col1:value, col2:value, col3:value, ... colX:value} to Array
    So I won't know "date" or "close" or whatever at runtime, I'll just have X number of items for each element in the array. How would this be done?
    The reason I need this mapping feature is because I then want to map the table-like array as the data provider to charts, datagrids, etc.
    Thanks.

    If this post answers your question or helps, please mark it as such.
    You can download the lib for JSON in Flex here:
    http://code.google.com/p/as3corelib/downloads/list
    These pages describe using JSON and Flex, and databases:
    http://www.switchonthecode.com/tutorials/flex-php-tutorial-transmitting-data-using-json
    http://www.switchonthecode.com/tutorials/using-flex-php-and-json-to-modify-a-mysql-databas e
    http://www.switchonthecode.com/tutorials/flex-php-json-mysql-advanced-updating
    http://www.mikechambers.com/blog/2006/03/28/tutorial-using-json-with-flex-2-and-actionscri pt-3/

  • Store int values into dynamic array

    Hello all,
    I'm a newbie in JAVA whose want to store int values (coming from a resultset) into an dynamic array. I know that normally to create a "dynamic array" you'd better use a Vector, but it seems that I don't know how to use it correctly (throws me a exception as : Incompatible type for method. Can't convert int[] to java.lang.Object[]).
    Thanks in advance
    STF

    I want to use the copyInto method of the vector, but it throw me an exception as ')' expected
    I include a snip of my code below ;
    Vector v= new Vector();
    Vector vv=new Vector();
    String[] labels=new String[v.size()];
    Integer[] values=new Integer[vv.size()];
    cont=rs.getInt("COUNT(CASE_ID_)");
    vv.add(j,new Integer(cont));
    log=rs.getString("ORIG_SUBMITTER");
    v.add(j,new String(log));
    v.copyInto(String[] labels);
    vv.copyInto(Integer[] values);

Maybe you are looking for

  • Little bit of thread and little bit of instant messenger design problem

    OK! i have a small problem here, i am planning on making a instant messenger server and client, not compatible with any out there(as of yet) and not using any stansard protocols out there, just something i made on my own. ok now the problem i am tryi

  • Horizontal Scrolling

    Hey guys, I'm trying to make a section of my web page scroll horizontally. I have found several working examples of this but they all make the entire <body> tag scrollable. I want just the blue outlined section in the following image to scroll and fo

  • Requesting .llb with 14 .vi's from 5.1 to 2013

    Hello again, I am delighted how active and helpful this community is. I am requesting a library containting 14 .vi's made in labview 5.1 to be converted to be compatable with labview 2013. The library of interest is attached. Cheers. Solved! Go to So

  • Add a classpath dinamically

    In my application i know at run-time the classpath and the name of the classes to call. For example, i have a class "com.mypackage.MyClass" located in "c:\JavaCode\com\mypackage\". I want to add dinamically this classpath and create an istance of the

  • SAP HCM Post Certification Process

    HI, I have just cleared my SAP Certification and I have not received any mail from SAP confirming the same neither have I received any link to download the logo. Can anyone help me with this issue.