Moulay Driss

Greenhorn

Posts: 5

posted 4 years ago

(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 =

or

2) T = (1,1) (2,2)

or

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

I used the method

1. increment all `x` of object elements with loop for

2. `Ta[0] = (6,6)`

3. `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.

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

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

//------- 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

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:1. increment all `x` of object elements with loop for

2. `Ta[0] = (6,6)`

3. `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.

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

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

//------- 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)
posted 4 years ago

Unless this is a homework assignment, consider using a data structure that isn't an array/arraylist. A map would let you have constant access by key.

Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2

posted 4 years ago

- 1

you'll usually server yourself better by writing code that is easy to follow than uses every possible trick to save time. If you use enough tricks and save yourself a microsecond each time, but spend and extra eight hours debugging this at some point int the future, your code is going to have to run a LOT to make up that lost time.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Moulay Driss

Greenhorn

Posts: 5

posted 4 years ago

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 =

or

2) T = (1,1) (2,2)

or

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

I used the method

1. increment all `x` of object elements with loop for

2. `Ta[0] = (6,6)`

3. `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.

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

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

//------- 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

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:

1. increment all `x` of object elements with loop for

2. `Ta[0] = (6,6)`

3. `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.

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

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

//------- 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)

posted 4 years ago

That's not a very well-defined spec, but if it is true, you will quite probably get the most bang for your buck if you buy a faster server. That would have the added bonus of speeding up EVERYTHING on the box, not just this one tiny piece.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Moulay Driss wrote:My objective is to minimize as possible the time of incrementation process

That's not a very well-defined spec, but if it is true, you will quite probably get the most bang for your buck if you buy a faster server. That would have the added bonus of speeding up EVERYTHING on the box, not just this one tiny piece.