Add new data into Array

 

I want to ask someone how to code below.


New data 1 comes.

I want to add into Array.

[1]

Then next a new data 3 comes.

[2,1]


Then next a new data 3 comes.

[3,2,1]

etc...


Can you help?

 

Normally Arrays are extended to the right

[1] --> [1,2] --> [1,2,3] ...


Why you want to make it the other way round?

 
Florian Riedrich #: Normally Arrays are extended to the right
  1. Arrays are not extended to the right or to the left. The direction of higher indexes are only in the mind of the coder.

    If value 3 is at position two, You want to extend to higher indexes. Easy:

    TYPE Array[];
    
    TYPE function(){
       // get value
       int iLast = ArraySize(Array);        // End
    
       ArrayResize(Array,iLast+1);          // Make room
    
       Array[iLast] = value;                // Store at new
    
  2. Cromo: Then next a new data 3 comes. [3,2,1]
    If the value 3 is at index zero, you want to extend and shift (a deque or stack). You need just three extra lines.
    TYPE Array[];
    
    TYPE function(){
       // get value
       int iLast = ArraySize(Array);        // End
       
       // This enlarges the arr and shifts values B[2]=B[1]; B[1]=B[0]; B[0]=prob 0.
       bool     isSeries = ArrayGetAsSeries(Array);   
       ArraySetAsSeries(Array, !isSeries);
          ArrayResize(Array,iLast+1);          // Make room
       ArraySetAsSeries(Array, isSeries);
    
    
       Array[iLast] = value;                // Store at new
    
    

 

Thank you!

Let me study this. You teach me many things. Arigatou.

 
William Roeder #:
  1. Arrays are not extended to the right or to the left. The direction of higher indexes are only in the mind of the coder.

    If value 3 is at position two, You want to extend to higher indexes. Easy:

  2. If the value 3 is at index zero, you want to extend and shift (a deque or stack). You need just three extra lines.
Interesting. Thanks
Reason: