"JAVA programming ideas" study notes: Chapter 16 (array)

Contents
Java programming ideas (a) 1 to 4 chapters: Overview of
Java programming ideas (b) Chapter 5: initialization and cleanup
Java programming ideas (c) Chapter 6: Access
Section (d) of the 7 Java programming ideas: Complex with class
Java programming ideas (e) Chapter 8: polymorphism
Java programming ideas (six) Chapter 9: Interface
Java programming ideas (seven) Chapter 10: internal class
chapter (h) of the 11 Java programming ideas: holding the object
thinking in Java (nine) Chapter 12: abnormal
thinking in Java (x) Chapter 13: string
chapter (XI) of 14 Java programming ideas: type information
(xii) Chapter 15 Java programming ideas: generic
Java Chapter 16 programming ideas (XIII): array

Chapter XVI, array

Accessed by integer index array elements, and the size can not be changed (characteristic); TNA or other containers using an array holding the object when required.

1. Why array of special

The difference between an array of other types of containers are threefold: efficiency, type and basic type of ability to save.

  • Efficiency: In Java, the array is a most efficient random access memory and object reference sequence of functions.
  • Type: Array reason better than the generic container before, because you can create an array to hold some specific type. This means you can pass compiler checks to prevent improper insertion of the wrong type and extraction type. - This advantage has been lost!
  • Save basic types: Due to the mechanism of automatic packaging container can hold basic types. - This advantage has been lost!

Array only remaining advantage is efficiency. However, if you want to solve the problem more general, that the array may be subject to too many restrictions, so in these cases you will still use the container.

 

2. Arrays are first-class objects

  • Array identifier: No matter what type of array using the array identifier is only a reference to the real object in a heap created, the array of objects to hold references to other objects.
  • Read-only members length: array object method or field is the only one that can be accessed. [] Syntax is the only way to access an array of objects.
  • Array initialization: When you generate a new array object, all references automatically initialized to null.

 

3. Return an array object

In Java, you just return an array directly, without having to worry about an array is responsible - as long as you need it, it will always exist, when you use finished, the garbage collector will clean out it.

eg: public String [] flavorSet () {return results;} // C ++ pointer can only return an array, the array can not return directly.

 

4. Multidimensional Arrays

  • Multidimensional array: For multidimensional arrays of primitive types, you can use curly braces to each vector separated, each enclosed in braces collection will take you to the next level array.
int[][] a = {{1,2,3},{4,5,6},}
System.out.println(Arrays.deepToString(a)); //Arrays.deepToString() 方法将多维数组转换为多个 String。
//Output:[[1,2,3],[4,5,6]]
  • Rough arrays: each vector in the matrix array may have any length (length).

5. Generic Array and

  • Examples of & generic array: Typically, the arrays do not integrate well generic. Not instantiate an array having a parameterized type.

Peel<Banana>[] peels = new Peel<Banana>[10]; //illegal

The reason: Erase removes the parameter type of information, but the array must know the exact type that it holds

  • Type & generic array itself: However, it allows the creation of a reference to that array. And, even though you can not actually create an array of objects held generics, but you can create an array of non-generic, then transformation.
List<String>[] ls;  //legal 
class ClassParameter<T>{
    public T[] f(T[] arg){ return arg;}//legal 
    public T[] g(int size){ return (T[]) new Object[size];}//legal 
}

6. Create test data

Generating an array of tools to facilitate the filling of the test data.

6.1 Arrays.fill()

This method is very limited and can only be used to fill each position with a value, for the purposes of the object is populated with a reference copy. Or an area filled array.

String[] a = new String[6];
Arrays.fill(a,"Hello")
print(Arrays.toString(a));      //[Hello,Hello,Hello,Hello,Hello,Hello]
Arrays.fill(a,3,5,"World")
print(Arrays.toString(a));      //[Hello,Hello,Hello,World,World,Hello]

6.2 data generator

  • Strategy Mode: Generator specific implementation of different classes, respectively, to achieve different counting methods. Counting means different types of data class code implementation details, see the book P443.

6.3 create an array from the Generator

public interface Generator<T>{
    T next();  //策略模式 or 模板方法?
}

public static class BooleanCount implements Generator<java.lang.Boolean>{
    public java.lang.Boolean next(){
        return r.nextBoolean();
    }
}

@SuppressWarnings("unckecked") //屏蔽compile warning
public static <T> T[] array(Class<T> type ,Generator<T>gen,int size){
    //Array.newInstance:借由传入Class对象创建对应类型元素的数组
    T[] a =  (T[]) java.lang.reflect.Array.newInstance(type,size); 
    return new CollectionData<T>(gen,size).toArray(a);
}

 

7.Arrays practical function

Arrays can be found in class java.util class library.

  • equals (): for comparing two arrays are equal;
  • fill (): filling an array;
  • binarySearch (): it has been used to find elements in order to make a good array;
  • toString (): String generated an array representation;
  • hashCode (): generates a hash code of the array;
  • Arrays.asList (): accepting an arbitrary sequence or an array as its parameter, and into the container List.

7.1 Copy array

System.arrayCopy () use it to copy an array much faster than the for loop. Its parameters are: the source array, indicates the offset from the beginning of the copy source array in what position, indicates the offset from the beginning of copying what position the target array, and the number of elements to be copied.

  • Shallow copy: shallow copy: System.arrayCopy () array copying objects, just copied object references, not the copy of the object itself;
  • Deep copy: deep copy:

 

7.2 Comparison of the array

  • Arrays.equals (): Arrays class provides the equals method overloaded, the entire array for comparison. Equal conditions: length of the array is equal to the corresponding index & element are equal.

7.3 Comparison of array elements

  • Custom comparison algorithm: to achieve java.lang.Comparable <T> interface, so you have the ability to compare class custom.
  • Arrays.sort (): Array automatic sorting
interface Comparator<T>{
    int compare(T o1,T o2);
}

class CompTypeComparator implements Comparator<CompType>{
    @Override
    public int compare(CompType o1,CompTypeo2){ 
        return (“自定义比较算法”);
    }
}

7.4 Array sorting (sorting built using arrays)

  • Arrays.sort (a, String.CASE_INSENSITIVE_ORDER): Use the built-sorting method, the basic type of the array can be any sort; may be any sort of object array, as long as the object implements the interface Comparable or has an associated Comparator .

7.5 Find a sorted array

  • Arrays.binarySearch (): If the array is already sorted, and you can use Arrays.binarySearch () to perform a quick search.
Published 217 original articles · won praise 72 · views 960 000 +

Guess you like

Origin blog.csdn.net/cbk861110/article/details/104106453