Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 4 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Dynamic Array Allocation

At 5:02 AM on Nov 5, 2006, krishna wrote:

http://www.javabeat.net/javabeat/scjp2/ht/index.php?examType=SCJP&msgId=14

Java does not offer arrays that can be extended at run time, for primitives or Objects, so it is common to use a Vector for a set of elements that needs to be able to grow. Once the set is complete, if your code needs repeated access to the elements, perhaps to sort them, it would be more efficient to be able to manipulate an array. Fortunately, it is easy to obtain an array from a Vector. Suppose you have placed numerous Strings in a Vector. To obtain the corresponding String array takes just three lines of code:


int count = yourVector.size();
String[] yourArray = new String[count];
yourVector.copyInto(yourArray);

If the Vector is not referenced again, the garbage collector will be able to recover the memory it required.
1 . At 10:52 PM on Nov 5, 2006, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Dynamic Array Allocation

It's probably more appropriate to use standard collection API methods (see Collection.toArray(T[] type) ).
Best, R.J. Lorimer
2 . At 2:18 AM on Nov 6, 2006, Carsten Saager wrote:
  Click to reply to this thread Reply

Re: Dynamic Array Allocation

Why using a Vector? Vector is clunky and synchronized. Use ArrayList and you can get the String[] by

String[] yourArray = yourList.toArray(new String[]{});

Which does what you want in a single line.

Also the ArrayList is quite efficient, in pre Java 5 the array had been preferred because they offer a typed collection, Collection does now the same without the limitation imposed by array
3 . At 11:32 AM on Nov 9, 2006, Will Hartung DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Dynamic Array Allocation

to be pednatic, the best way to do this is:

String[] yourArray = (String[])yourList.toArray(new String[yourList.size()]);


That preallocates the array to the right size. At a minimum it will save you a constructor.
4 . At 2:51 AM on Feb 15, 2007, Israr Ahmed wrote:
  Click to reply to this thread Reply

Re: Dynamic Array Allocation

There is a facility for dynamic array allocation in java by using either list or Vector.
By using any of these you can make your array dynamic.
There certainly have been performance issues with Java. We've been working really hard on them. The primary way we've attacked the problem is with advanced virtual machines. The performance has been getting very nice. --James Gosling, 1999.

thread.rss_message