• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best class to implement extensible arrays

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm new to Java, coming over from years of SAP ABAP coding.

I can see that arrays have to be declared as fixed at construction time.

What if I want to extend the size of the array at run time? What are generally accepted classes to use for such operations? (I assume that there are some). I would like to extend and possibly cut elements from the middle of the 'array', maintaining the order, to access an element by a key (not necessarily the index), and add to the end.

Oh yes, would also like it to be fast!

Kind regards,

Tony.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the Collections Framework classes in java.util.*
[ March 21, 2005: Message edited by: Junilu Lacar ]
 
Tony Bateman
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will do,

Thanks for the quick reply!

Kind regards,

Tony.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a Crib Sheet I use to keep my favorite collections straight.
 
Tony Bateman
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that too!

One question: on your web page you state:

"It is good practice to declare variables and parameter types with the interfaces and not the concrete classes".

Why is that?

Cheers,

Tony.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Bateman:
"It is good practice to declare variables and parameter types with the interfaces and not the concrete classes".

Why is that?



Consider the case where you create an implementation using some collection. Later you wish to change the semantics of the implementation, as the order of elements becomes important.

It is much easier to slot in a replacement instantiation of a concrete implementation that guarantees some order, in one place, than it is to go through all of your code, replacing method declarations etc.

Most code doesn't care about details, instead it only cares that some methods are available.

The item order example is quite specific, but writing your code to an interface rather than a concrete class is good practice in general.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Bateman:
Thanks for that too!

One question: on your web page you state:

"It is good practice to declare variables and parameter types with the interfaces and not the concrete classes".

Why is that?

Cheers,

Tony.


This paradigm is known as "coding to the interface not the implementation" (or something like that) and is a common way of thinking in any Object-Oriented programming language. I think Horatio's gives a good abstract explanation of the concepts, but I'd like to add a (perhaps) more concrete example.

Let's say you are working on a project where you need to index items with something other than an integer. Other languages provide this ability with some built-in type. After looking at the Collections framework in Java, you see that the
Map interface provides this functionality. You then need to choose a specific class that implements this. The typical choices are between TreeMap, which can order the elements, or HashMap, which does not preserve any specific order. Since ordering the elements adds some overhead and your particular project does not care about the order, you decide to use HashMap. Then you go about declaring variables, parameters, and return types with the HashMap class.

Now let's say that the requirements or design changes so that your application DOES care about ordering the elements. Now you have to go through all of your code and change HashMap to TreeMap. Obviously this will take some time and be a potential for errors.

If on the other hand, you originally declared all variables, parameters, and return types with the Map interface type, then you would only need to change a few places where you create the object! Think of the time savings this makes.

Admitedly, this example is somewhat contrived. However, I think it illustrates the benefites of using the interface instead of a particular concreate class. There are also other benefits, but I'll let you research it more if you are interested.

Keep Coding!

Layne
 
Tony Bateman
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the last two posts; this really looks like a terrific place to post questions and get intelligent answers. Those posts have given me a good place to direct current studies. I'll mull these replies over until I get it!

Thanks heaps!

Tony.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic