• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Karen Haq
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks. Ugh, I see where I missed the info. I need more coffee!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!