- 浏览: 72028 次
- 性别:
- 来自: 北京
文章分类
最新评论
copy from http://www.janeg.ca/scjp/lang/arrays.html
Array declarations
- arrays are Java objects
- all Java arrays are technically one-dimensional. Two-dimensional arrays are arrays of arrays.
- declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array
- array variable declarations must indicate a dimension by using []
Examples of valid array declarations: (JJ pg84) String[]s; String []s; String [] s; String [ ] s; // extra white space ignored String[] s; String[ ] s; // extra white space ignored String s[]; String s []; String s [ ]; // extra white space ignored String[] s[]; String[][]s; String s [] [ ]; // extra white space ignored
- declaring the size of the array with the following notation is illegal
String[5] s; // illegal declaration
- the standard convention
for declaring arrays is:
String[] s; // one-dimensional array String[][] s; // two-dimensional array
Initializing arrays
- all arrays are zero-based
- arrays must be indexed by int values or byte, short or char values (as these can be promoted to int) (JLS §10.4)
- using a long index value to access an array causes a compile error
- attempting to access an array with an index less than 0 or greater than the length of the array causes an ArrayIndexOutOfBoundsException to be thrown at runtime (JLS §10.4)
- since arrays are Objects they can be initialized using the new operator
- when created, arrays are automatically initialized with the default value of their type
String[] s = new String[100]; // default values: null boolean[] b = new boolean[4]; // default values: false int[] i = new int[10][10]; // default values: 0
- array references declared as members are initialized to null BUT array references declared in methods are not initialized
class TestArray {
int[] arr; // member declaration, initialized to 'null'
public static void main(String[] args) {
int[] arr1; // reference variable 'arr1' not initialized
// compiles ok
System.out.println("arr:" + new TestArray().arr);
// compile error
System.out.println("arr1: " + arr1);
}
}
- as arrays are allocated at runtime, you can use a variable to set their dimension
int arrSize = 100; String[] myArray = new String[arrSize];
- you can use curly braces {} as part of an array declaration to initialize the array
String[] oneDimArray = { "abc","def","xyz" };
Note |
String[] s;
// illegal initialization
s = { "abc", "def", "hij");
int[] arr = new int[] {1,2,3}; // legal
|
- you can assign an array a null
value but you can't create an empty array by using a blank index
int[] array = null; // legal // illegal initialization int[] array = new int[];
Initializing two-dimensional arrays
- the first dimension represents the rows, the second dimension, the columns
- curly braces {} may also be used to initialize two dimensional arrays. Again they are only valid in array declaration statements.
int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} };
- you can initialize the row dimension without initializing the columns but not vice versa
int[][] myArray = new int[5][]; // illegal int[][] myArray = new int[][5];
- the length of the columns can vary
class TestTwoDimArrays { // initialize # of rows static int [][] myArray = new int[3][]; public static void main(String[] args) { myArray[0] = new int[3]; // initialize # of cols myArray[1] = new int[4]; // in each row myArray[2] = new int[5]; for(int i=0; i<3; i++) // fill and print the array fillArray(i, i+3); System.out.println(); } // end main() private static void fillArray(int row, int col) { for( int i=0; i<col; i++) myArray[row][i] = i; for( int i=0; i<col; i++) System.out.print(myArray[row][i]); System.out.println(); } } Output of TestTwoDimArrays: 012 0123 01234
Tips
- array index operator [] has highest level of precedence
- integer variables can be used as array dimension values
Traps
- incorrect array declaration statements, particularly:
arrayType [#] varName; - incorrect array initialization statements, particularly:
arrayType[] varName = new arrayType[2];
varName = { value, value, value }; - negative values for array index
- long value for array index
- array declaration used as an array creation statement
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 587well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 398http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 661this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 745http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 697http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 835What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 769http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 709I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 670http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 714http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1675http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 833Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 901The XML declaration typically a ... -
UCM
2010-05-08 11:41 746Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 768refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 843As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 936copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 817copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 866It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1309copy from http://blog.cornetdes ...
相关推荐
《 Beginning Java 8 Fundamentals & Language Features》是Kishori Sharan的作品,旨在帮助读者深入理解和掌握Java 8的基础知识和语言特性。这两本书详细介绍了Java 8的新特性和改进,为初学者提供了全面的学习资源...
Allocating Storage for Multidimensional Arrays 5.6.2.4 - Accessing Multidimensional Array Elements in Assembly Language 5.6.3 - Structures 5.6.4 - Arrays of Structures and Arrays/Structures ...
Learn the basics of Java 9, including basic programming concepts and the object-oriented fundamentals necessary at all levels of Java development. Author Kishori Sharan walks you through writing your ...
Move into iOS development by getting a firm grasp of its fundamentals, including the Xcode IDE, the Cocoa Touch framework, and Swift 2.0—the latest version of Apple's acclaimed programming language....
Necaise introduces the basic array structure and explores the fundamentals of implementing and using multi-dimensional arrays. The underlying mechanisms of many of Python’s built-in data structures ...
Move into iOS development by getting a firm grasp of its fundamentals, including the Xcode IDE, the Cocoa Touch framework, and Swift 3 the latest version of Apple s acclaimed programming language....
9. **First-Class ADTs**: This section introduces the concept of first-class ADTs, which are ADTs that can be treated like any other object in the language. This feature allows for greater flexibility ...
Arrays 90 Chapter 4: Objects and Classes 105 Introduction to Object-Oriented Programming 106 Using Predefined Classes 111 Defining Your Own Classes 122 Static Fields and Methods 132 Method ...
This friendly and easy-to-use self-study guide will help you understand the fundamentals of this core programming language. ☆ 出版信息:☆ [作者信息] Stephen Prata [出版机构] Sams Publishing ...
This tutorial focuses on fundamentals and introduces a wide range of useful techniques, including: Basic Python programming and scripting Numerical arrays Two- and three-dimensional graphics Monte ...
In the start of this book, we will be revising the C++ language fundamentals that will be used throughout this book. We will be looking into some of the problems in arrays and recursion too. Then in ...
Covers debugging, errors, comparisons with C++ and Java, classes and arrays, variables, and more Includes access to a companion website with sample code and bonus materials Everything you need to make...
This chapter covers the fundamentals of the Python language, including basic types, control flow, defining functions, and object-oriented programming concepts. - **Basic Types:** Integers, floating-...
In the start of this book, we will be revising the C++ language fundamentals that will be used throughout this book. We will be looking into some of the problems in arrays and recursion too. Then in ...