JDK-4090307 : resizing arrays
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.1.4
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_95
  • CPU: x86
  • Submitted: 1997-11-03
  • Updated: 1997-11-03
  • Resolved: 1997-11-03
Related Reports
Relates :  
Description

Name: bk70084			Date: 11/03/97


Array resizing is something programmers often need
to do.

Right now, the best way to do it is:

(1) allocate a new array and assign it to a new
    temporary variable
(2) using the System.arraycopy method, copy the
    elements from the original array into the new
    array
(3) assign the handle of the new array to the old
    variable

This is tedious.

Would it be cool if an array's "length" property 
was writable?

int [] ar = new int [5];
...use ar for a while...
...decide it's time to make it an array of 10 elements...
ar . length = 10;
/* done */

I think this is an interesting idea worth pursuing,
and it would not compromise backward compatibility.

Another alternative is to add a resize () method
to all arrays.
(Review ID: 19257)
======================================================================

Comments
WORK AROUND Name: bk70084 Date: 11/03/97 int [] ar = new int [5]; ...use a for a while... ...decide it's time to make it an array of 10 elements... int [] temp_ar = new int [10]; System . arraycopy (ar, 0, temp_ar, 0, ar . length); ar = temp_ar; /* done */ ======================================================================
11-06-2004

EVALUATION The problem with a writable length field is that it requires an indirection in the VM. The way things are now, the array header that contains the length field can be contiguous in memory with the array elements themselves. This is possible because a new array must be created in order to perform a resize. If arrays could be resized in place without reassignment to the variable that holds the reference to them, indirection would have to be used within the representation of the array. This indirection would make array accesses slower. A resize() method that returned a new array would be possible. Note that the new standard collection classes in 1.2 that auto-extend are likely to be a better choice for many existing uses of arrays. david.stoutamire@Eng 1997-11-03
03-11-1997