Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Java Array Revealed_2

In Java array of characters is not a String :



In Java programming language an array of char is  neither a String nor an array of char is terminated by '\u0000'  (the NULL character).
A String object is immutable, that is, its contents never change, while an array of
char has mutable elements,But the length of array is final.

Java Array Revealed_1

IN the Java programming language, arrays are objects ,and  dynamically
created, and may be assigned to variables of type Object. All methods of
class Object may be invoked on an array.

Let's talk about Array variables:

A variable of array type holds a reference to an object. Declaring a variable of array
type does not create an array object or allocate any space for array components. It

creates only the variable itself, which can contain a reference to an array.
And interesting thing is about the array declaration style.

Example:

byte[ ] set, get, wet[ ];

This declaration is equivalent to:


byte set[ ], get[ ], wet[ ] [ ];


It is not recommend  "mixed notation" in an array variable declaration, where

brackets appear on both the type and in declarators

Array Mutability

Just as with object mutability, you need to be aware of array mutability. First look this example below.


public class ShippingInfo {
private static final String[] states = {
"AK", "AZ", "CA", "DE", "NV", "NY"};
}

The following code fragment could be used to iterate through the list of states and print them out.
for (int i = 0; i < ShippingInfo.states.length; i++) {
System.out.println(ShippingInfo.states[i]);
}
This is easy enough, but using the final keyword with arrays can be tricky. The following code does not
compile as you might expect.
ShippingInfo.states = new String[50];
It fails because you cannot assign values to final variables. The following code, however, is perfectly legal:
ShippingInfo.states[5] = "Java City";
This code replaces the entry for  ("NV") with "Java City." Obviously, final arrays are not
immutable, and passing them around can violate encapsulation. No syntax in the Java programming language
provides a truly immutable array. This can lead to all kinds of inconsistencies. To avoid these problems, one
solution is to make the following changes:
1. Make the array private.
2. Add a getStates method.
3. Return a copy of the states array from the getStates method.
                                               
                                                  OR



you can use Iterator as inner class to increase the performance of the program.

Example:

public class ShippingInfo {
private static final String[] states = {
"AK", "AZ", "CA", "DE", "NV", "NY"};
public static Iterator getStates() {
return new StateIterator();
}
public static class StateIterator implements Iterator {
private int current = 0;
/* from Iterator */
public boolean hasNext() {
return current < states.length;
}
/* from Iterator */
public Object next() {
return nextState();
}
/* from Iterator */
public void remove() {
throw new UnsupportedOperationException();
}
/* custom typesafe next */
public String nextState() {
if (current < states.length) {
String state = states[current];
current++;
return state;
} else {
throw new NoSuchElementException();
}
}
}

}



The following code snippet can be used to iterate through the array safely, without concern that it could be accidentally damaged as read only.
Iterator iter = ShippingInfo.getStates();
while (iter.hasNext()) {
System.out.println(iter.next());



 
Copyright (c) 2013 Java Discovery.