Results 1 to 4 of 4

Thread: Why are Arrays Object [Sort of Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    65

    Why are Arrays Object [Sort of Resolved]

    I understand that everything in Java is an object. But what are the advantages of Arrays being objects too? Any special significance to it?
    Last edited by onbliss; May 28th, 2003 at 04:22 PM.

  2. #2
    Lively Member Bootking's Avatar
    Join Date
    Mar 2003
    Location
    Marquette University
    Posts
    90
    When an array is an object the advantage is that you can access its length property and you can use .equals(array) to compare it to another array. Consider this, for some unknown reason I am cycling through some colors in an applet.

    Color[] clrs = {Color.red, Color.blue, Color.green};
    for (int i=0; i<clrs.length; ++i)
    {
    //Do some junk
    }

    You can edit the array without having to edit the rest of the program to accompany it.
    Check out my band's website!
    Walter Walker Band

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    An array treated and manipulated as an object in Java not only gains the advantages that BootKing has pointed out but also given an arbitrary object x, you can find out if the object x is an array and, if so, what type of array it is.

    Code:
     Class type =  x.getClass();
    
     if(type.isArray()){
       Class elementType = type.getComponentType();
     }

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Being an object also allows arrays to be stored in collections like ArrayList.
    And it allows them to be passed as object, as is necessary because an int[] (or other arrays of basic types) can't be cast to Object[], so the System.arraycopy method would need to be overloaded instead of accepting Object arguments.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width