1

There is an array lut_addresses[] of type int. There are some calculations for a variable table_ptr which is also an int and represents the new base of the array. Now i want to assign lut_addresses[] values beginning from the index table_ptr till the last index to the array lut_addresses[] so that initial values till table_ptr are deleted and value at table_ptr is present at 0th index of lut_addresses[]. How can i do it without changing lut_addresses to an arraylist?

Pseudo code:

A()
{
   int lut_addresses[] = new int[2048];
   // assign values upto a cetain index
   B(lut_addresses);
};
B()
{
   int table_ptr=0;
   //calculate table_ptr;
   // assign lut_addresses[] values from index table_ptr till (lut_addresses.length-1)  
}  
2
  • to which the values of lut_addresses need to be assigned Commented Mar 20, 2012 at 11:20
  • See my edit -- System.arraycopy is the best choice for you. Commented Mar 20, 2012 at 11:40

7 Answers 7

4

First thing that comes to mind would be to use System.arraycopy.

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to clarify, every way to do this in Java would be equivalent to an array copy. You can't directly manipulate pointers in Java like you could in C or C++.
1
for (int i = startIndex; i < 2048; i++) {
  lut_addresses[i] = newValue;
}

1 Comment

I haven't seen any other arrays in your question. But you may just place anything you want instead of newValue. For example lut_addresses[i] = source[j]. You'll need to increment j as well of course. But for this task System.arraycopy() would be more convenient.
1

You could either

  • Use a for loop that runs over the desired indices, assigning the elements one at a time, or
  • Use the System.arraycopy method, which copies a given number of elements starting at a given offset in one array to a given offset in another array.

Based on your later edits, I should point out that System.arraycopy correctly handles overlapping source and destination regions, making it an excellent choice for you.

3 Comments

I maybe wrong but this method won't remove the elements, and i will get the array with correct elements till index lut_addresses.length-table_ptr-1 and the remaning elements will remain as they were at their original place. E.g if lut_addresses[]=[1,2,3,4,5,6,7,8,9,10}, i will get lut_addresses[]={4,5,6,7,8,9,10,8,9,10}
You keep changing the question, man, this is ridiculous. It used to say something about assigning part of an array, then about copying elements, now it's about removing part of an array. I'm out.
I am trying something in my project. Thats why so many changes. Hope you don't mind
0

You can't change an array's size once it's allocated, but you can store in a variable the size of the "used" part (this is close to what ArrayList does internally). So, you could use System.arraycopy() to copy some part of one array into other and use a new int variable to store the size of the filled in part. Then, when you iterate, use that variable instead of lut_addresses.length as the upper iteration limit.

Comments

0

I think you could use an IntBuffer. from the documentation :

Relative bulk put methods that transfer contiguous sequences of ints from an int array or some other int buffer into this buffer

Comments

0

There is no direct method to directly get the specified element as in ArrayList, so you have to first loop to search element and then replace each element after this with (index -index of searched element).

Comments

0

you can use System.arraycopy that in built-in java standard, see http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.