|
WebObjects 5.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--com.webobjects.foundation.NSArray
NSArray and its subclass NSMutableArray manage ordered collections of objects. NSArray creates immutable arrays and NSMutableArray creates mutable arrays.
The following table describes the NSArray methods that provide the basis for all NSArray's other methods; that is, all other methods are implemented in terms of these three. If you create a subclass of NSArray, you need only ensure that these base methods work properly. Having done so, you can be sure that all your subclass's inherited methods operate properly.
Method |
Description |
count |
Returns the number of elements in the array. |
objectAtIndex |
Provides access to the array elements by index. |
objectsNoCopy |
Returns a natural language array containing the NSArray's objects. |
The methods objectEnumerator and reverseObjectEnumerator grant sequential
access to the elements of the array, differing only in the direction of
travel through the elements. These methods are provided so that arrays can
be traversed in a manner similar to that used for objects of other collection
classes in both the Java API and in the Foundation framework, such as java.util.Hashtable
or NSDictionary. See the objectEnumerator method description for a
code excerpt that shows how to use these methods to access the elements of
an array.
NSArray provides methods for querying the elements of the array. indexOfObject
searches the array for the object that matches its argument. To determine
whether the search is successful, each element of the array is sent an
equals message. Another method, indexOfIdenticalObject, is
provided for the less common case of determining whether a specific object
is present in the array. indexOfIdenticalObject tests each element in
the array to see if it is the exact same instance as the argument.
To act on the array as a whole, a variety of other methods are defined.
You can extract a subset of the array (subarrayWithRange) or concatenate
the elements of an array of Strings into a single string
(componentsJoinedByString). In addition, you can compare two arrays using the
equals and firstObjectCommonWithArray methods. Finally, you can
create new arrays that contain the objects in an existing array and one or
more additional objects with arrayByAddingObject and
arrayByAddingObjectsFromArray.
List,
ArrayList,
Vector,
count(),
objectAtIndex(int),
indexOfObject(java.lang.Object),
arrayByAddingObjectsFromArray(com.webobjects.foundation.NSArray),
componentsJoinedByString(java.lang.String),
objectEnumerator(), Serialized Form| Inner Class Summary | |
static interface |
NSArray.Operator
Objects implementing NSArray.Operator are used to perform operations on an array's elements. |
| Inner classes inherited from class com.webobjects.foundation.NSCoding |
NSCoding.Support |
| Inner classes inherited from class com.webobjects.foundation.NSKeyValueCoding |
NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.Null, NSKeyValueCoding.UnknownKeyException, NSKeyValueCoding.Utility, NSKeyValueCoding.ValueAccessor |
| Inner classes inherited from class com.webobjects.foundation.NSKeyValueCodingAdditions |
NSKeyValueCodingAdditions.DefaultImplementation, NSKeyValueCodingAdditions.Utility |
| Field Summary | |
static String |
AverageOperatorName
A key representing the operator ( NSArray.Operator) that computes the average of the elements in an array. |
static String |
CountOperatorName
A key representing the operator ( NSArray.Operator) that computes the number of elements in an array. |
static NSArray |
EmptyArray
A constant representation of an empty array. |
static String |
MaximumOperatorName
A key representing the operator ( NSArray.Operator) that computes the largest element in an array. |
static String |
MinimumOperatorName
A key representing the operator ( NSArray.Operator) that computes the smallest element in an array. |
static int |
NotFound
This constant is returned by several search methods, indicating that no matching object was found. |
static String |
SumOperatorName
A key representing the operator ( NSArray.Operator) that computes the sum of the elements in an array. |
| Fields inherited from interface com.webobjects.foundation.NSKeyValueCoding |
NullValue |
| Fields inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions |
KeyPathSeparator |
| Constructor Summary | |
NSArray()
Creates an empty, immutable array. |
|
NSArray(NSArray otherArray)
One should use immutableClone instead. |
|
NSArray(Object object)
Creates an array containing the single element object. |
|
NSArray(Object[] objects)
Creates an array containing the elements from objects. |
|
NSArray(Object[] objects,
NSRange range)
Creates an array containing the objects from objects in the range
specified by range. |
|
NSArray(Vector vector,
NSRange range,
boolean ignoreNull)
Creates an array containing the objects from vector in the range
specified by range. |
|
| Method Summary | |
NSArray |
arrayByAddingObject(Object object)
Returns a new NSArray which contains the values from this array, and appends the object
argument. |
NSArray |
arrayByAddingObjectsFromArray(NSArray otherArray)
Returns a new NSArray which contains the values from this array, and appends values from the otherArray argument. |
Class |
classForCoder()
Conformance to the NSCoding interface. |
Object |
clone()
Clones this array Object Since NSArrays are immutable, there's no need to make an actual clone. |
String |
componentsJoinedByString(String separator)
This method concatenates the String representation of each element in this array together in order from lowest index to highest. |
static NSArray |
componentsSeparatedByString(String string,
String separator)
This static method tokenizes the string argument into a series of substrings according to separator
It creates a new NSArray which contains the substrings in the order they appeared in string. |
boolean |
containsObject(Object object)
This method determines whether object is present
in this array by comparing it to each of the array's objects using equals |
int |
count()
|
static Object |
decodeObject(NSCoder coder)
Creates a new NSArray from the data in coder. |
void |
encodeWithCoder(NSCoder coder)
Encodes the receiver using coder. |
boolean |
equals(Object object)
Compares this array to object, returning
true if object satisfies instanceof NSArray
and its elements are equal to the elements of this array, otherwise it returns false. |
Object |
firstObjectCommonWithArray(NSArray otherArray)
This method searches for the first object in this array which is contained within otherArray. |
void |
getObjects(Object[] objects)
Deprecated. use public Object[] objects() instead |
void |
getObjects(Object[] objects,
NSRange range)
Deprecated. use public Object[] objects() in NSRange instead |
int |
hashCode()
|
NSArray |
immutableClone()
Since an NSArray is immutable, NSArray's implementation simply returns this. |
int |
indexOfIdenticalObject(Object object)
Searches all objects in this array for object (testing for
equality by comparing object references directly using the
== operator, not the equals method.)
and returns the first (lowest) index
whose corresponding element is identical to object. |
int |
indexOfIdenticalObject(Object object,
NSRange range)
Searches the specified subset within this array for object
(testing for equality by directly comparing Object references with ==) and returns the first
(lowest) index whose corresponding element is identical to object. |
int |
indexOfObject(Object object)
Searches this array for object and returns the
first (lowest) index whose corresponding element is equal to object. |
int |
indexOfObject(Object object,
NSRange range)
Searches the specified subset within this array for object and
returns the first (lowest) index whose corresponding element is equal
to object. |
boolean |
isEqualToArray(NSArray otherArray)
Included for backward compatibility. |
Object |
lastObject()
|
void |
makeObjectsPerformSelector(NSSelector selector,
Object[] parameters)
Invokes the method specified by selector on each object in this
array. |
NSMutableArray |
mutableClone()
NSArray's implementation creates a new NSMutableArray with this array's elements, not copies. |
Object |
objectAtIndex(int index)
Returns the Object reference stored at index within this NSArray. |
Enumeration |
objectEnumerator()
This method creates a java.util.Enumeration which can used to iterate through the array's elements. |
Object[] |
objects()
|
Object[] |
objects(NSRange range)
Copies a subset of this array into a Java array. |
protected Object[] |
objectsNoCopy()
This method should only be used by subclasses. |
static NSArray.Operator |
operatorForKey(String operatorName)
|
static NSArray |
operatorNames()
Returns the names of the operations that can be performed on array elements. |
static void |
removeOperatorForKey(String operatorName)
Removes the operator identified by operatorName from the list of
operators that can be performed on array elements. |
Enumeration |
reverseObjectEnumerator()
This method creates a java.util.Enumeration which can used to iterate through the array's elements in reverse. |
static void |
setOperatorForKey(String operatorName,
NSArray.Operator arrayOperator)
Registers an operator with a String. |
NSArray |
sortedArrayUsingComparator(NSComparator comparator)
This method creates a new array with this array's element which have been sorted in an order based on the comparator operation. |
NSArray |
sortedArrayUsingSelector(NSSelector selector)
Deprecated. use sortedArrayUsingComparator instead |
NSArray |
subarrayWithRange(NSRange range)
This method creates a new array from a subset of this array. |
void |
takeValueForKey(Object value,
String key)
Conformance to the NSKeyValueCoding interface. |
void |
takeValueForKeyPath(Object value,
String keyPath)
Sets the value for the property identified by keyPath
to value. |
String |
toString()
|
Object |
valueForKey(String key)
Conformance to the NSKeyValueCoding interface. |
Object |
valueForKeyPath(String keyPath)
Conformance to the NSKeyValueCodingAdditions interface. |
Vector |
vector()
Creates a java.util.Vector with the same contents as this object. |
| Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
public static final String AverageOperatorName
NSArray.Operator) that computes the average of the elements in an array.public static final String CountOperatorName
NSArray.Operator) that computes the number of elements in an array.public static final NSArray EmptyArray
public static final String MaximumOperatorName
NSArray.Operator) that computes the largest element in an array.public static final String MinimumOperatorName
NSArray.Operator) that computes the smallest element in an array.public static final int NotFound
public static final String SumOperatorName
NSArray.Operator) that computes the sum of the elements in an array.| Constructor Detail |
public NSArray()
EmptyArraypublic NSArray(Object object)
object.
object is not permitted to be nullobject - the single element within the new NSArrayjava.lang.IllegalArgumentException - if object is nullpublic NSArray(Object[] objects)
objects.
objects must not be null,
but this method ignores any null values within objects.objects - a Java array containing the initial values for
the newly created NSArray
public NSArray(Object[] objects,
NSRange range)
objects in the range
specified by range.
Ignores any null values it encounters in objects.objects - a Java array containing initial values for the
newly created NSArrayrange - specifies which elements of the objects
array are to be addedpublic NSArray(NSArray otherArray)
immutableClone instead.
Creates a new array that contains the same object references as
otherArray.
otherArray must not be null.otherArray - the array to be duplicatedimmutableClone()
public NSArray(Vector vector,
NSRange range,
boolean ignoreNull)
vector in the range
specified by range.
The ignoreNull parameter controls the method's behavior
when it encounters a null value in the vector. If
ignoreNull is true, then null values
are simply ignored. If ignoreNull is false,
the method raises an IllegalArgumentException when it encounters a null
value.vector - the Java.util.Vector of elements to be included in the new NSArrayrange - the range of objects to includeignoreNull - whether to ignore null values in vector or throw exceptionIllegalArgumentException - if a null
element exists in vector.Vector| Method Detail |
public NSArray arrayByAddingObject(Object object)
object
argument.
If object is null, an IllegalArgumentException is thrown.object - the object to be appendedobject
appendedIllegalArgumentException - if object is nullSee Also: NSMutableArray.addObject(java.lang.Object)public NSArray arrayByAddingObjectsFromArray(NSArray otherArray)
otherArray argument.otherArray - the array to be appendedotherArray appended.NSMutableArray.addObjectsFromArray(com.webobjects.foundation.NSArray)public Class classForCoder()
classForCoder in interface NSCodingNSCoding.Support.classForCoder(java.lang.Object),
NSCoding,
NSMutableArraypublic Object clone()
clone in class ObjectthisNSMutableArray,
immutableClone(),
mutableClone(),
Object.clone()public String componentsJoinedByString(String separator)
separator String is placed in between each element.
This is in all aspects the inverse function of componentsSeparatedByString
For example, this code excerpt writes the path System/Developer
to the console:
Each element in the receiver's array must handle eitherNSArray pathArray = new NSArray(new Object [] {{"System ","Developer "}); System.out.println("The path is "+pathArray.componentsJoinedByString("/")+".");
description,
or if it is not implemented, toString.
If the receiver has no elements,
a String representing the empty string is returned.separator - the string to be inserted inbetween each element.
It may be any String object, including null or an empty Stringseparator between
the elements of this arraycomponentsSeparatedByString(java.lang.String, java.lang.String)
public static NSArray componentsSeparatedByString(String string,
String separator)
string argument into a series of substrings according to separator
It creates a new NSArray which contains the substrings in the order they appeared in string.
If string begins or ends with separator, then the first or last substring, respectively, is an empty String.
If string is null, or an empty String, then this method returns an NSArray with 0 elements.
If separator is null, or an empty String, then it returns an NSArray with 1 element, the string Object.
This method is in all aspects the inverse function of componentsJoinedByString
A code excerpt:
String list ="wrenches, hammers, saws"; NSArray listItems = NSArray.componentsSeparatedByString (", ");
produces an array with these contents:
Index |
Substring |
| 0 | wrenches |
| 1 | hammers |
| 2 | saws |
If list is instead
then the result is:String list =", wrenches, hammers, saws";
Index |
Substring |
| 0 | (empty string) |
| 1 | wrenches |
| 2 | hammers |
| 3 | saws |
If list has no separators, for example, "wrenches"
the array contains the string itself, in this case "wrenches".
string - the input string to be parsedseparator - the string that is to separate the substringsstring that have
been divided by separatorcomponentsJoinedByString(java.lang.String)public boolean containsObject(Object object)
object is present
in this array by comparing it to each of the array's objects using equalsobject - the searched for objecttrue if this array contains an object equal to object, otherwise falsepublic int count()
public static Object decodeObject(NSCoder coder)
coder.
This is the inverse function of encodeWithCodercoder - the coder from which to retrieve the array's dataNSCodingpublic void encodeWithCoder(NSCoder coder)
NSCodingcoder. Object type information along with an
object's data is stored.encodeWithCoder in interface NSCodingcom.webobjects.foundation.NSCodingcoder - an NSCoder object that will be used to encode object of classes that
implement this interfaceNSCoderpublic boolean equals(Object object)
object, returning
true if object satisfies instanceof NSArray
and its elements are equal to the elements of this array, otherwise it returns false.
Two arrays have equal contents if they each hold the same number
of objects and objects at corresponding indices in each array satisfy the
equals test.equals in class Objectobject - the object to compare this array againsttrue if object is an NSArray and its contents
are equal to this array or false otherwiseObject.equals(java.lang.Object)public Object firstObjectCommonWithArray(NSArray otherArray)
otherArray. It uses equals to check for equality.otherArray - the input arrayotherArray, or null if no such
object is foundpublic void getObjects(Object[] objects)
public Object[] objects() instead
objects - the destination buffer to copy this array into. It must be large enough
to hold count Object references.objects()
public void getObjects(Object[] objects,
NSRange range)
public Object[] objects() in NSRange instead
objects - the destination buffer to copy a subset of this array into.
It must be large enough to hold count Object references.range - the subset of entries to copy into objectsobjects(NSRange range)public int hashCode()
hashCode in class ObjectObject.hashCode()public NSArray immutableClone()
this. Subclasses such as NSMutableArray should override
this method to create an immutable copy of this array.NSMutableArray,
mutableClone(),
clone()public int indexOfIdenticalObject(Object object)
object (testing for
equality by comparing object references directly using the
== operator, not the equals method.)
and returns the first (lowest) index
whose corresponding element is identical to object.
If none of the objects in the receiver are identical to object,
this method returns NotFound.object - the search's targetobjectNotFound
public int indexOfIdenticalObject(Object object,
NSRange range)
object
(testing for equality by directly comparing Object references with ==) and returns the first
(lowest) index whose corresponding element is identical to object.
If none of the objects in the range match, then
this method returns NotFound. Throws an IllegalArgumentException
if range is out of bounds.object - the search's targetrange - the subset to searchobjectjava.lang.IllegalArgumentException - if range is out of boundsNotFoundpublic int indexOfObject(Object object)
object and returns the
first (lowest) index whose corresponding element is equal to object.
Objects are considered equal if equals returns true. If
none of the specified objects are equal to object, returns NotFound.object - the search's targetanObjectNotFound
public int indexOfObject(Object object,
NSRange range)
object and
returns the first (lowest) index whose corresponding element is equal
to object.
Objects are considered equal if equals returns true. If none of
the specified objects are equal to object, returns NotFound.
Throws an IllegalArgumentException if aRange is out of bounds.object - the search's targetrange - the subset to searchequals to objectIllegalArgumentException - if aRange is out of bounds.public boolean isEqualToArray(NSArray otherArray)
equals instead.otherArray - the array to be compared againsttrue if otherArray and this array are
equal else falseequals(java.lang.Object)public Object lastObject()
null if the array is empty
public void makeObjectsPerformSelector(NSSelector selector,
Object[] parameters)
selector on each object in this
array.
The method is invoked each time with the values in parametersas the
method's arguments. The method should not, as a side effect, modify this array or its elements.selector - the method to invoke. selector must not be nullparameters - the arguments to that methodNSSelector.invoke( Object target, Object[] arguments)public NSMutableArray mutableClone()
NSMutableArray,
immutableClone(),
clone()public Object objectAtIndex(int index)
index within this NSArray.
If the array is empty or if index is beyond the end of the array
(that is, if index is greater than or equal to the value returned
by count), an IllegalArgumentException is thrown.
Negative index arguments are similarly illegal.index - the location of the objectindexIllegalArgumentException - when index is not greater than
or equal to 0 and less than countcount()public Enumeration objectEnumerator()
java.util.Enumeration enumerator = myArray.objectEnumerator(); while (enumerator.hasMoreElements()) { Object anObject = enumerator.nextElement(); //code to act on each element }
When this method is used with mutable subclasses of NSArray, your code shouldn't modify the array during enumeration.
reverseObjectEnumerator()public Object[] objects()
public Object[] objects(NSRange range)
range - the range specifying a subset to copyprotected Object[] objectsNoCopy()
public static NSArray.Operator operatorForKey(String operatorName)
operatorName - the human-readable name for the operator instanceoperatorName
if one exists, null otherwiseNSArray()public static NSArray operatorNames()
| Key | Operator Description |
count |
Returns the number of elements in an array. |
max |
Returns the element in the array with the highest value. |
min |
Returns the element in the array with the lowest value. |
avg |
Returns the average of the array's elements' values. |
sum |
Returns the sum of the array's element's values. |
public static void removeOperatorForKey(String operatorName)
operatorName from the list of
operators that can be performed on array elements.operatorName - the human-readable name for the operator
instance to be removedpublic Enumeration reverseObjectEnumerator()
java.util.Enumeration enumerator = myArray.reverseObjectEnumerator(); while (enumerator.hasMoreElements()) { Object anObject = enumerator.nextElement(); //code to act on each element }
When this method is used with mutable subclasses of NSArray, your code shouldn't modify the array during enumeration.
objectEnumerator()
public static void setOperatorForKey(String operatorName,
NSArray.Operator arrayOperator)
IllegalArgumentException if either operatorName or
arrayOperator are null.operatorName - the human-readable name for the operator instancearrayOperator - the operator instanceIllegalArgumentException - if either
operatorName or
arrayOperator are null
public NSArray sortedArrayUsingComparator(NSComparator comparator)
throws NSComparator.ComparisonException
comparator operation.comparator - It determines the ordering in the new sorted array by comparing two elements at a time and
returning one of OrderedAscending, OrderedSame or OrderedDescending- Returns:
- a new sorted array that lists this array's elements in an order determined
by
comparator - Throws:
NSComparator.ComparisonException - if the comparator's compare
method throws for any reason- See Also:
NSComparator.compare(java.lang.Object, java.lang.Object)
public NSArray sortedArrayUsingSelector(NSSelector selector)
throws NSComparator.ComparisonException
sortedArrayUsingComparator instead
selector - an NSSelector for the method to compare the elements during the sortsortedArrayUsingComparator(com.webobjects.foundation.NSComparator)public NSArray subarrayWithRange(NSRange range)
range - the subset of elements to be included in the new arrayrangejava.lang.IndexOutOfBoundsException - when input range
is outside the bounds of this array
public void takeValueForKey(Object value,
String key)
takeValueForKeyPath on each element in this array
with the arguments value and key
For example, if key is "firstName" and value is "Unknown", this method sets the firstName property
of each element to "Unknown".takeValueForKey in interface NSKeyValueCodingvalue - each element's new value for the keypathkey - the keypath for the property to set on each elementNSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String)
public void takeValueForKeyPath(Object value,
String keyPath)
NSKeyValueCodingAdditionskeyPath
to value. A key path has the form relationship.property
(with one or more relationships); for example "movieRole.roleName"
or "movieRole.talent.lastName". The default implementation of
this method (provided by NSKeyValueCodingAdditions.DefaultImplementation)
gets the destination object for each relationship using valueForKey,
and sends the final object a takeValueForKey message with value
and property.takeValueForKeyPath in interface NSKeyValueCodingAdditionscom.webobjects.foundation.NSKeyValueCodingAdditionsvalue - the property identified by keyPath is set to thiskeyPath - identifies the property of an objectNSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String),
NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String),
NSKeyValueCodingAdditions.DefaultImplementationpublic String toString()
toString in class Objectpublic Object valueForKey(String key)
key indicates an operation
that doesn't require an argument, valueForKey performs the operation and returns the result.
key indicates an operation if its first character is "@". For example,
if key is "@count", valueForKey invokes compute on the "count"
operator. This has the effect of computing and returning the number of elements
in the receiver. Don't use valueForKey for operations that take
arguments; instead use valueForKeyPath.valueForKey creates a new array with the
same number of elements as this array. For each element, the corresponding element in the new array is the result
of invoking valueForKeyPath with key as the key path on the element. For example, if key is "firstName",
this method returns an array containing the firstName values for
each of the array's elements. The key argument can be a key path of the
form relationship.property. For example, "department.name".
valueForKey replaces null values with an instance of NSKeyValueCoding.Null.valueForKey in interface NSKeyValueCodingkey - the input keyvalueForKeyPath on each element of this arrayNSKeyValueCoding.valueForKey(java.lang.String),
operatorNames(),
valueForKeyPath(java.lang.String),
NSKeyValueCoding.Nullpublic Object valueForKeyPath(String keyPath)
keyPath indicates an operation
takes an argument (such as computing an average), valueForKeyPath
performs the operation and returns the result. key indicates an
aggregate operation if its first character is "@". For example,
if key is "@avg.salary", valueForKey invokes compute on
the "avg" operator specifying the array and "salary" as arguments. This
has the effect of computing and returning the average salary of
the array's elements.valueForKeyPath behaves similarly to valueForKey and
produces a new NSArray whose elements correspond to the results of invoking valueForKeyPath
on each element of this array.valueForKeyPath in interface NSKeyValueCodingAdditionskeyPath - the keypath to the elements in the input arrayvalueForKeyPath on each element of this arrayvalueForKey(java.lang.String),
NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String),
operatorNames()public Vector vector()
|
Last updated Thu Jan 10 18:10:21 PST 2002. | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||