This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

transparently expanding array  RSS feed

 
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.
 
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.
 
author
Sheriff
Posts: 23484
138
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: 23484
138
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!