Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

usefulness of arrays... 4

Status
Not open for further replies.

acent

Technical User
Feb 17, 2006
247
US
Alright... I've read that arrays are one of the most usefull items in programming... my question is WHY???

Let me explain, I can see why a list of items can be benefitial, but I've never found a good use for one. Is this due to my inexperience or a few twisted wires upstairs? The reason I have never found a use for one is because they seem clumsy and inflexible. Say for example, you have a program to store contacts. If you count all your contacts and then create an array for fast searching, what happens when you need to add one more? or delete one? The array you have created is now either not big enough or too big and wasting memory.

Maybe this is a bad example. If so and arrays are really usefull, can someone provide me an example or explanation? Stacks, hashmaps/tables, etc seem like very useful tools. They grow, they shrink. Any ideas?

Thanks,

Andrew

"God is a comedian playing to an audience too afraid to laugh."
-- Francois Marie Arouet (Voltaire)
 
Sometimes you don't have growing or shrinking collections (i.e.: monthnames), and there arrays are perfectly legal.

But if you read 'usefull in programming' - perhaps the book wasn't about java especial?

Ask the author.

seeking a job as java-programmer in Berlin:
 
Thanks for the reply. Is this to say that arrays are only useful when the list cannot grow?

Also, why would you want to put the names of 12 months in an array? Can you grow on that example?

Thanks,

Andrew

"God is a comedian playing to an audience too afraid to laugh."
-- Francois Marie Arouet (Voltaire)
 
Array is very useful for two dimension or more to use.

you can have:

int a[][] or int a[][][][][][][].

For example, in a chess game, you need to store a serveral possible number to control a chessman.

Chinese Java Faq Forum
 
Well - the monthnames-example isn't the best one, because normally we would do it with the Calendar-class and with localization.
Here is a better one:
Code:
Exception ex = new Exception ("intended and unthrown!");
StackTraceElement [] stea = ex.getStackTrace ();
for (StackTraceElement ste : stea)
{
        System.out.println (ste);
}

seeking a job as java-programmer in Berlin:
 
I agree an array is not the more flexible structure, but has a list of key advantages:

- easy to create, fill and manipulate.
- almost every language has the concept of array and a similar way to manipulate it
- intuitive
- multidimensional
- free of overhead introduce by other classes like ArrayList

And there are ways to add rows for an array, you can create one bigger and copy the data :)

Cheers,
Dian
 
Thanks to all of you for your input on this issue. However I have one question for Diancecht, is this how you might increase an array?

Code:
class myApp {
   public static void main(String[] args) {
      String[] myArray[5];
      //fill array with bogus data
      myArray[0] = "test0";
      myArray[1] = "test1";
      myArray[2] = "test2";
      myArray[3] = "test3";
      myArray[4] = "test4";
      myArray = growArray(myArray);
   }
   public static String[] growArray(String[] a){
      String[] tempArray[a.length + 1]; //create a temp hodler
      for(int i = 0; i <= a.length; i++) { //fill the temp array with the data from the array recieved
         tempArray[i] = a[i]
      }
      return tempArray;
   }
}
My code may not be totally legit but is this the general idea?

Thanks,

Andrew

"God is a comedian playing to an audience too afraid to laugh."
-- Francois Marie Arouet (Voltaire)
 
Arrays offer advantages in certain circumstances. For instance, if you have a method that must return a group of primatives (ints, longs), there's considerable overhead in wrapping those primatives in objects to include in a collection - even when the jvm is doing it for you as in java 1.5. An array of ints is much quicker and easier.

Another situation is when you are returning a group of things that must be in a specific order. Most collections do not guarantee that the order of the elements coming out will be the same as the order they were put in. To implement such a guarantee - using a TreeSet, etc adds more overhead. An example of this might be a byte array holding a jpg image. The bytes must be in that specific order.

There are also times when you don't want the caller of your object to add or subtract from the group of things you've created. The byte array of an image is a good example of this as well. You don't want the user adding more bytes to the end or inserting some in the middle, because the resulting jpg would be corrupted. So the array is not only faster and easier, it implicitly says "This is not a collection that should be modified".
 
By the way, the method System.arrayCopy claims to have a good performance to copy arrays.

Cheers,
Dian
 
Thank you all for the posts. I see now that I was origionally thinking of wrong uses for arrays.

Thanks,

Andrew

"God is a comedian playing to an audience too afraid to laugh."
-- Francois Marie Arouet (Voltaire)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top