Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: add and increment array elements by arraycopy

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add and increment array elements by arraycopy

    suppose an array of object: Element T[ ] , each object contains two integer variables (x,y).

    T = (1,1) (2,2) (3,3) (4,4)
    I want to increment the value of the variable x of object each time a new element is added to array with the faster way possible . The new element can be added in any position and we increment all x element after the insertion position (position +1)

    Before add (6,6) :

    T = (1,1) (2,2) (3,3) (4,4)

    After add (6,6) in different positions:

    1) T = (6,6) (2,1) (3,2) (4,3) (5,4)

    or

    2) T = (1,1) (2,2) (6,6) (4,3) (5,4)

    or

    3) T = (1,1) (2,2) (3,3) (6,6) (5,4)

    I used the method arraycopy to add the new element, and loop for to increment the variable x for each element, as follow:

    increment all x of object elements with loop for

    Ta[0] = (6,6)

    araycopy(T, 0, Ta, 1, T.size-1 );

    because it is faster than

    While (i< T.length){

    T[i] = T[i+1]

    T[i].x ++;

    i++;
    }
    I need to add the new element and increment the other objects of array simultaneously with a faster time.

    //-------------------
    public class elemt {
     
    public int x;
    public int y;
     
    public elemt(int a, int b){
        this.x= a;
        this.y= b;
    }
     
     
    public void inc(){
     x++;
    }
     
    int getX(){
        return x;
    }
     
    int getY(){
        return y;
    }
    }
    //----------------

    public class TAD {
     
    public static ArrayList < elemt > T = new ArrayList < elemt > ( );
     
    public static ArrayList < elemt > T1 = new ArrayList < elemt > ( );
     
     public static void main(String[] args){
     
     
     
        for(int i=0; i<10000000; i++){
           T1.add(new elemt(i, i));
          }
     
     
         long t0 = System.currentTimeMillis();
     
          T1.add(0, new elemt(1, 1));
     
         long t1= System.currentTimeMillis()- t0;
     
         System.out.println("Time without Incrementation : "+t1);
     
     
     //--------------
     
         for(int i=0; i<10000000; i++){
           T.add(new elemt(i, i));
          }
     
     
        long t2 = System.currentTimeMillis();
     
           T.add(0, new elemt(1, 1));
     
            for(int i=1; i<T.size(); i++){
              T.get(i).inc();
            }
     
        long t3= System.currentTimeMillis()- t2;
     
      System.out.println("Time with Incrementation: "+t3);
     
     }
    //------- The results:

    Time without Incrementation : 15 ms

    Time with Incrementation: 156 ms

    My objective is to minimize as possible the time of incrementation process

    (Time with Incrementation < Time without Incrementation * 2 )

    because actually

    Time with Incrementation (156 ms) = Time without Incrementation (15 ms )* 10

    I notes that i can added a new element in any position, but i chose the worst case (adding an element in the first position that requires the incrementation of all x element of the arraylist)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: add and increment array elements by arraycopy

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add and increment array elements by arraycopy

    I need to add the new element and increment the other objects of an array of objects (elemt) simultaneously with a faster time.




     public class elemt {
     
            public int x;
            public int y;
     
            public elemt(int a, int b){
                this.x= a;
                this.y= b;
            }
     
     
            public void inc(){
             x++;
            }
     
            int getX(){
                return x;
            }
     
            int getY(){
                return y;
            }
     
     
        }

    //----------------


       public class TAD {
     
           public static ArrayList < elemt >  A1 = new ArrayList < elemt > ( ); // first array
     
           public static ArrayList < elemt > A2 = new ArrayList < elemt > ( ); // second array
     
             public static void main(String[] args){
     
           //------- first test -------------- 
     
                for(int i=0; i<10000000; i++){
                   A1.add(new elemt(i, i));    // fill array A1 
                  }
     
     
                 long t0 = System.currentTimeMillis();
     
                  T1.add(0, new elemt(1, 1));  // calculate te time of adding one element in filled array A1
     
                 long t1= System.currentTimeMillis()- t0;
     
                 System.out.println("Time first test : "+t1);
     
     
     
     
           //----------- Second Test ------------
     
                 for(int i=0; i<10000000; i++){
                   A2.add(new elemt(i, i));     // fill array A2
                  }
     
     
                long t2 = System.currentTimeMillis();
     
                   A2.add(0, new elemt(1, 1)); // adding one element in filled array A2
     
                    for(int i=1; i<T.size(); i++){
                      A2.get(i).inc();     // Increment all element of A2 from position 1.
                    }
     
                long t3= System.currentTimeMillis()- t2;
     
              System.out.println("Time second test: "+t3);
     
     
     
             }
    //------- The results:

    Time without Incrementation : 15 ms

    Time with Incrementation: 156 ms


    My objective is to minimize as possible the time of incrementation process

    (Time second test < Time first test * 2 )

    because actually

    Time second test (156 ms) = Time first test (15 ms )* 10

    I notes that i can added a new element in any position, but i chose the worst case (adding an element in the first position that requires the incrementation of all **x** element of the arraylist)

    I tried many data structure as vector, hashmap ,... but I could not minimize the time of incrementation

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: add and increment array elements by arraycopy

    I could not minimize the time of incrementation
    Try rewriting the code in machine language.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add and increment array elements by arraycopy

    Quote Originally Posted by Norm View Post
    Try rewriting the code in machine language.
    I tried with JNI (Java Native Interface) but no improvement !!!!!

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: add and increment array elements by arraycopy

    You could probably drastically cut that time down by using an iterator to loop through the array list rather than get(<index>).

    Also may I see your JNI Implementation?

    After that you are pretty much bound by the fact the algorithm is always going to be O(n).

    Regards,

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  7. #7
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: add and increment array elements by arraycopy

    I think a problem is... arraycopy doesn't iterate through to copy the data. When you create an array, you set aside a block of memory the size of the array multiplied by the size of the data type size, but only keep the first sets memory address. And when you array[1] for example, you are using the array[0] memory block address plus 1 multiplied by the data type, plus of memory address of array[0]. array[10,000] would refer to memory address array[0] + 10,000 x dataTypeSize.

    For example. An object containing int x, int y would take up 8 bytes. Creating an array of 10,000 this object you would get a pointer, that points to memory address "0xblah". And it would set aside a memory block of "0xblah+80,000". If you you call object[5], it would give you a pointer that refers to memory address "oxblah + 40".

    Point is, I think arraycopy just sets aside a new memory block and gives you a pointer, it does not iterate through. The only way to increment x would be to iterate through, which would be (0)n. But the arraycopy you are essentially getting for 1.

    On second thought, I think it is likely you get (0)n for arraycopy, but for a a malloc instruction, rather than an iterative assignment.
    Last edited by theoriginalanomaly; May 8th, 2013 at 03:08 PM. Reason: Adding second thought

  8. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: add and increment array elements by arraycopy

    I don't get what you are timing with your profiling above...the first represents the time it takes to add a single element, the second time represents the time it takes to a) loop over the entire List and for each element 1) get the element 2) call a method 3) which increments a variable - apples and oranges IMO.

Similar Threads

  1. unable to add elements in jlist
    By shanalikhan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 16th, 2013, 04:22 PM
  2. [SOLVED] Char Array Increment + 1
    By Nuggets in forum What's Wrong With My Code?
    Replies: 38
    Last Post: April 13th, 2012, 05:35 AM
  3. [SOLVED] How To Increment Array Elements
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: April 1st, 2012, 12:10 PM
  4. Java App - Add, Delete, Reorder elements of the buttons
    By alibm in forum AWT / Java Swing
    Replies: 5
    Last Post: March 7th, 2011, 07:46 AM
  5. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM