Newbie Java array question

Status
You're currently viewing only Solomonoff's Secret's posts. Click here to go back to viewing the entire thread.
Not open for further replies.
Blacken00100":2loxcnir said:
The array construct is a valuable one,
Yes, in that it allows ArrayList to be implemented in terms of it. It's rare that you need to use arrays (directly). You should virtually always use collection classes. (Unfortunately, for non-blocking atomic operations on list elements, you sometimes have to use Atomic*Array classes, which thinly wrap arrays' shitty API.)

and isn't really a "primitive" at all
Except that you can't implement it from scratch in Java code, so in that sense it's primitive. Conceptually, it's primitive because it's low-level.
 
If you're using multiple threads to store elements in a single list, chances are that accessing the list in a thread-safe manner doesn't buy you much as usually you want thread safety in a coarser-grained manner. For example, if you call hasNext() on an iterator, you may want to be sure that the list still has a next element when you subsequently call next(), or if you see that the list is not empty and then remove the first element, you want to know that another thread didn't just remove the first element.
 
Right. It can be replaced by array1.clone(). The only point of doing this is so that you can modify one array independently of the other - not so that you can store more things in an array. To resize, you'd have to do something like:
Code:
static int[] resize(int[] orig, int newLength) {
  int[] neww = new int[newLength];
  System.arraycopy(orig, 0, neww, 0, Math.min(orig.length, newLength));
  return neww;
}
Basically, you create a new array and copy elements over from the old array to the new array.
 
Status
You're currently viewing only Solomonoff's Secret's posts. Click here to go back to viewing the entire thread.
Not open for further replies.