Declarations and Access Control (1)

wozhidao_yy

wozhidao_yy

2016-02-19 13:30

图老师设计创意栏目是一个分享最好最实用的教程的社区,我们拥有最用心的各种教程,今天就给大家分享Declarations and Access Control (1)的教程,热爱PS的朋友们快点看过来吧!

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.

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

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.

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

    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, '

展开更多 50%)
分享

猜你喜欢

Declarations and Access Control (1)

编程语言 网络编程
Declarations and Access Control (1)

Declarations and Access Control (2)

编程语言 网络编程
Declarations and Access Control (2)

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

用Access设计客观试卷(1)

编程语言 网络编程
用Access设计客观试卷(1)

JBL Control NOW 音箱

创意手工
JBL Control NOW 音箱

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Access 2007的Ribbon功能区详解(1)

编程语言 网络编程
Access 2007的Ribbon功能区详解(1)

DataGrid连接Access的快速分页法(1)——需求与现状

编程语言 网络编程
DataGrid连接Access的快速分页法(1)——需求与现状

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Excel/Access 97 网页制作速成

Excel/Access 97 网页制作速成

改变ACCESS数据库的密码

改变ACCESS数据库的密码
下拉加载更多内容 ↓