国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Declarations and Access Control (1)

2024-07-21 02:14:06
字體:
來源:轉載
供稿:網友


1) declarations and access control
objective 1
write code that declares, constructs and initializes arrays of any base type using any of the permitted forms, both for declaration and for initialization.

1.    arrays are java objects. (an object is a class instance or an array.)  and may be assigned to variables of type object.  all methods of class object may be invoked on an array.  if you create an array of 5 strings, there will be 6 objects created.
2.    arrays should be
1.    declared. (int[] a; string b[]; object []c;  size should not be specified now)
2.    allocated (constructed). ( a = new int[10]; c = new string[arraysize] )
3.    initialized. for (int i = 0; i < a.length; a[i++] = 0)
3.    the above three can be done in one step. int a[] = { 1, 2, 3 }; or  int a[] = new int[] { 1, 2, 3 }; but never specify the size with the new statement.
4.    java arrays are static arrays. size has to be specified at compile time. array.length returns array’s size, cannot be changed.  (use vectors for dynamic purposes).
5.    array size is never specified with the reference variable, it is always maintained with the array object. it is maintained in array.length, which is a final instance variable.  note that arrays have a length field (or property) not a length() method. when you start to use strings you will use the string, length method, as in  s.length();
6.    anonymous arrays can be created and used like this:  new int[] {1,2,3} or new int[10]
7.    arrays with zero elements can be created. args array to the main method will be a zero element array if no command parameters are specified. in this case args.length is 0.
8.    comma after the last initializer in array declaration is ignored.

    int[] i = new int[2] { 5, 10};  // wrong
int i[5] = { 1, 2, 3, 4, 5};  // wrong
int[] i[] = {{},  new int[] {} }; // correct
int i[][] = { {1,2}, new int[2] }; // correct
int i[] = { 1, 2, 3, 4, } ; // correct
int i[][] = new int [10][];  //correct,  i.length=10.
    int [] i, j[] == int i[], j[][];
9.    array indexes start with 0. index is an int data type.
10.    square brackets can come after datatype or before/after variable name. white spaces are fine. compiler just ignores them.
11.    arrays declared even as member variables also need to be allocated memory explicitly.  
static int a[];
static int b[] = {1,2,3};
public static void main(string s[]) {
    system.out.println(a[0]); // throws a null pointer exception
    system.out.println(b[0]); // this code runs fine
    system.out.println(a); // prints ‘null’
    system.out.println(b); // prints a string which is returned by tostring
}
12.    once declared and allocated (even for local arrays inside methods), array elements are automatically initialized to the default values.(0 for numeric arrays, false for boolean, '/0' for character arrays, and null for objects).
13.    if only declared (not constructed), member array variables default to null, but local array variables will not default to null(compile error).
14.    java doesn’t support multidimensional arrays formally, but it supports arrays of arrays. from the specification - “the number of bracket pairs indicates the depth of array nesting.” so this can perform as a multidimensional array. (no limit to levels of array nesting)
15.    arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion and become int values. an attempt to access an array component with a long index value results in a compile-time error.
16.    all array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an arrayindexoutofboundsexception to be thrown.
17.    every array implements the interfaces cloneable and java.io.serializable.
18.    array store exception
if an array variable v has type a[], where a is a reference type, then v can hold a reference to an instance of any array type b[], provided b can be assigned to a.
thus, the example:
class point { int x, y; }
class coloredpoint extends point { int color; }
class test {
    public static void main(string[] args) {
        coloredpoint[] cpa = new coloredpoint[10];
        point[] pa = cpa;
        system.out.println(pa[1] == null);
        try {
            pa[0] = new point();
        } catch (arraystoreexception e) {
            system.out.println(e);
        }
    }
}
produces the output:
true
java.lang.arraystoreexception
testing whether pa[1] is null, will not result in a run-time type error. this is because the element of the array of type coloredpoint[] is a coloredpoint, and every coloredpoint can stand in for a point, since point is the superclass of coloredpoint.
on the other hand, an assignment to the array pa can result in a run-time error. at compile time, an assignment to an element of pa is checked to make sure that the value assigned is a point. but since pa holds a reference to an array of coloredpoint, the assignment is valid only if the type of the value assigned at run-time is, more specifically, a coloredpoint. if not, an arraystoreexception is thrown.

19.    every element of an array must be of the same type the type of the elements of an array is decided when the array is declared. if you need a way of storing a group of elements of different types, you can use the collection classes.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊宁县| 吉木乃县| 新野县| 安岳县| 泉州市| 新营市| 曲周县| 阿瓦提县| 黑河市| 新田县| 安阳县| 格尔木市| 四子王旗| 栖霞市| 吉首市| 金门县| 北辰区| 三原县| 吉首市| 繁峙县| 拜泉县| 刚察县| 光泽县| 富平县| 定结县| 新巴尔虎左旗| 兴山县| 北海市| 昂仁县| 中江县| 高雄市| 九龙县| 施甸县| 察隅县| 丰城市| 南陵县| 连城县| 临朐县| 临朐县| 建湖县| 荣成市|