• Post Reply Bookmark Topic Watch Topic
  • New Topic

transparently expanding array  RSS feed

 
nida lulu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I want to transparently expand my array, but I have a 2D array to work with.
so i made a method like this:
[code]
private MusicCD[] doubleArray(MusicCD original[][]) {

int length = original.length;
MusicCD newArray[] = new MusicCD [length * 2];
System.arraycopy(original, 0, newArray, 0, length);
return newArray;
}
[code]
I guess the problem I am having is how to say anArray[6][] is the one that needs expanding not all.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use an ArrayList? It works just about the same as an array, as far as maintaining insertion order and such, but it grows dynamically so that you never have to expand your array manually.

If this is definitely not an option there are ways to do it but IMHO it is a bad design to do so.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How does this not throw an exception? You are trying to copy elements from one array to another -- but the element types are different.

Henry
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, I assumed it was a psudo-code method...

ps: How the heck do you spell psudo?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ps: How the heck do you spell psudo?


Yea, it does have a weird spelling...

It's spelled Pseudo.

Henry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!