• 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

Super simple question: When to use Class Array versus Class Arrays?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm almost there but my head hurts from rethinking this, so I'm asking you all for help, again. I'm only on Chapter 7 of Deitel's Java book, an introduction to Arrays and ArrayLists.

What is the difference between Class Array (java.lang.reflect.Array) versus Class Arrays (java.util.Arrays)? If I create a one dimensional primitive array, an array full of integers, for example, then can I apply the static methods from both of these classes to my primitive array? I don't see a Constructor in the Java API documentation for either class so all of these methods for not "reinventing the wheel", right?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.Arrays is a utility class. It has several methods that you can apply to arrays that are not part of arrays themselves. For instance, if you want to check if two int arrays are equal you call Arrays.equals(array1, array2). array1.equals(array2) only returns if array1 == array2 so it's mostly useless.

java.lang.reflect.Array is used in reflection only, and has no other purpose than to create new arrays, get array lengths, get array elements and set array elements of some object that may or may not be an array. In most code you will never use this class.
 
Karen Haq
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I think I'll be using the utility class way more often than the reflection class - which looks a little hairy to deal with.

One more question about Arrays Class. Look at this method:

static int binarySearch(Object[] a, Object key)
Searches the specified array for the specified object using the binary search algorithm.

Is this method taking an array full of Objects as its parameter? And what is Object key, the second parameter? I'm trying to understand the API as I'm learning Java.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Haq wrote:Is this method taking an array full of Objects as its parameter? And what is Object key, the second parameter? I'm trying to understand the API as I'm learning Java.


The documentation explains what those parameters are:

Parameters:
a - the array to be searched
key - the value to be searched for


The method checks if the object that key refers to is present in the array a. It returns the index of the object in the array if it was found, and a negative value otherwise. So you can check the return value of the method, and if it's less than zero, the object was not found in the array.

Note that the array must be sorted in order for this method to work properly; if the array is not sorted, you'll get undefined results.
 
Karen Haq
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Ugh, I see where I missed the info. I need more coffee!
reply
    Bookmark Topic Watch Topic
  • New Topic