Intermediate Arrays Anonymous arrays Java 2 added anonymous arrays, which allow you to create a new array of values anywhere in the program, not just in an initialization in a declaration. // This anonymous array style can also be used in other statements. String[] days = new String[] {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; You can also use anonymous array syntax in other parts of the program. For example, // Outside a declaration you can make this assignment. x = new String[] {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; You must be careful not to create these anonymous arrays in a loop or as local variables because each use of new will create another array. Dynamic allocation Because arrays are allocated dynamically, the initialization values may arbitrary expresssions. For example, this call creates two new arrays to pass as parameters to drawPolygon. g.drawPolygon(new int[] {n, n+45, 188}, new int[] {y/2, y*2, y}, 3); C-style array declarations Java also allows you to write the square brackets after the variable name, instead of after the type. This is how you write array declarations in C, but is not a good style for Java. C declaration syntax can get very ugly with part of the declaration before the variable, and part after. Java has a much nicer style where all type information can be written together without the use of a variable, and there are times when only the Java notation is possible to use. int[] a; // Java style -- good int a[]; // C style -- legal, but not Java style Converting between arrays and Collections data structures [TO DO] Common array problems Some common array programming mistakes are: Library methods for arrays Static methods for manipulating arrays are available in the java.util.Arrays class. Returns a List based on the array. Arrays.toString() Returns a readable form of the array. Arrays.binarySearch() Performs binary serach of a sorted array. Arrays.equals Compares two arrays for equality. Arrays.fill() Fills entire array or subrange with a value. Arrays.sort() Sorts an array. In addition there is System.arrayCopy() method. Anonymous arrays have to be assigned to the variables at the time of declaration of varaible itself. Assigning to the variable after variable declaration is not possible i=i++ produces the output "0″ instead of "1″. The code "i = i++" roughly translates to int oldValue = i; Array and ArrayList In a arrayList of object, any types of object(mixed) can be put. However in an array of objects, once initialized to a particular object type, other type of objects cannot be put. Thus, ArrayList a = new ArrayList(); is allowed and will run fine. However, Object[] obj = new String[5]; will throw ArrayStoreException(is a RunTimeException). With generics, arraylist is also not allowed to store Integer if it is declared as ArrayList of String. Thus, with generics, But with generics also, one more difference is array allows polymorphic assignment., i.e., Number[] n = new Integer[5]; However, in case of generics, polymorphic assignment holds good only for the base type and not for the generic type. i.e., List<Integer> l = new ArrayList<Integer>; — for base type hold good ArrayList<Number> n = new ArrayList<Integer>; — not allowed for generic type
ie.,
int[] i = {1,2,3} is fine.
But,
int[] i;
i = {1,2,3} is compiler error.
This restriction at the time of method invocation is valid, since we don't know the type of array(overloading issue). But for assignment the left side variable type is known. So, why should it be not possible to cast the anonymous array {1,2,3} to int[] and assign to variable.
int i = 0;
i = i++;
System.out.println(i);
produces the output "0″ instead of "1″.
i = i + 1;
i = oldValue;
a.add("krishna");
a.add(5);
obj[0] = new Integer(1);
ArrayList<String> a = new ArrayList<String>();
a.add("krishna");
a.add(5); — will be compiler error(unlike array which is at rutime).
ARRAYS - II
Posted by gautam at Saturday, December 15, 2007
Subscribe to:
Post Comments (Atom)
1 comments:
thanks
Post a Comment