什么是数组,如何声明和初始化数组。
如何访问数组中的单个元素。
如何使用数组中的单个元素。
如何声明嵌套数组。
如何建立可变长度的嵌套数组。
如何建立String对象。
如何建立并使用String对象的数组。
对String对象能够进行哪些操做。
什么是StringBuffer对象以及它们与String对象的操做有何关系。
对StringBuffer对象能够进行哪些操做。
4.1.1数组变量
数组变量和它所引用的数组是两个相互分离的实体。为数组变量所分配的内存空间保存着对数组对象的引用,而不是数组对象自己。
数组对象自己是一个存在于内存其余位置的独特实体。
全部引用对象的变量所存储的都是引用,引用记录着它们所引用的对象的内存地址。
声明数组变量:
int[] primes;与 int primes[]; 等价
4.1.2数组定义
primes = new int[10];
4.1.3数组的长度
primes.length
4.1.4访问数组元素
4.1.5数组变量的重用
int[] primes;
primes = new int[10];
...
primes = new int[50]; //先前包含10个元素的数组连同其中所存储的数据值都被丢弃。
4.1.6数组初始化
int[] primes = {2,3,5,7,11,13,17};
数组对象自己是可写的,即primes[3]=99是可行的,本觉得数组对象自己会在常量区(之前的模糊印象)。
1.使用实用方法初始化数组
double[] data = new double[50];
Arrays.fill(data, 1.0);这个方法适用任何基本数据类型的数组。
由于fill()是Arrays类中的一个静态方法,因此能够:
import static java.util.Arrays.fill;
fill(data, 1.0);
2.初始化数组变量
long[] even={2L,4L,6L,8L,10L};
long[] value = even;
两个数组变量引用同一个数组。
4.1.7使用数组
对数组使用collection-based for循环
double[] samples = new double[50];
...
double average = 0.0;
for(double value : samples){
average += value;
}
average /= samples.length;
4.1.8嵌套数组
4.1.9字符数组
4.2字符串
4.2.2 建立String对象
String对象被称做是永久的,也就是说它们不能够改变。
4.2.3 字符串数组
4.3字符串的运算
4.3.1字符串的链接
4.3.2字符串的比较
== 将判断两个String变量是否引用同一个字符串,即比较的是地址。
1.字符串相等性的比较
string1.equals(string3) 对大小写敏感
string1.equalsIgnorecase(string3) 忽略大小写
2.字符串扣留
字符串扣留可以确保不存在封装有彻底相同字符串的两个String对象,所以全部的String对象封装的字符串对象都唯一。String string1 = "Too many ";String string2 = "cooks";String string3 = "Too many cooks";string1 += string2;string1 = string1.intern();string1 == string3 的求值结果为true,由于引用同一个对象。字符串扣留有两个好处。第一,减小保存String对象所须要的内存总量。第二,当但愿比较两个字符串的相等性时容许使用“==”操做符来代替equals()方法。3.检测字符串的开始和结束string1.startsWith("Too");string1.endsWith("cooks");4.3.3字符串的排序string1.compareTo(string3)4.3.4访问字符串中的字符String text = "To be or not to be, that is the question; Whether";int textLength = text.length();for(int i = 0; i<textLength; i++) char ch = Character.toLowerCase(text.charAt(i));Character.isLetter(ch)Character.isWhitespace(ch)4.3.5搜索字符串中的字符text.indexOf('a');text.lastIndexOf('a');text.indexOf('a',startIndex);4.3.6子串搜索4.3.7提取子串text.substring(5);text.substring(7,11); //注意,从index = 7开始,到index = 11 - 1;切分字符串split()String[] words = text.split("[, .]",0);以逗号、空格、句号为分隔符。4.3.8 String对象的修改版本String newText = text.replace(' ', '/');String sample = " This is a string ";String result = sample.trim();4.3.9 由String对象建立字符数组String text = "To be or not to be";char[] textArray = text.toCharArray();String text = "To be or not to be";char[] textArray = new char[3];text.getChars(9,12,textArray, 0);4.3.10 对字符串使用collection-based for循环程序中不能够直接使用String对象做为collection-based for循环的取值范围,可是可使用数组做为其取值范围。所以,toCharArray()方法就提供了一种使用collection-based for循环遍历字符串中字符的方法。String phrase = "The quick brown fox jumped over the lazy dog.";int vowels = 0;for(char ch : phrase.toCharArray()){ ch = Characger.toLowerCase(ch); ...}4.3.11 以字节数组的形式获取字符串中的字符String text = "To be or not to be";byte[] textArray = text.getBytes(); 只存放8位的字符4.3.12 由字符数组建立String对象String类还有一个静态方法 copyValueOf(),能够从char[]类型数组建立String对象。char[] textArray = {'T','o',' ','b','e',' ','o','r',' ','n','o','t',' ','t','o',' ','b','e'};String text = String.copyValueOf(textArray);String text = new String(textArray);String text = String.copyValueOf(textArray, 9, 3);String text = new String(textArray, 9, 3);4.4 可变字符串String对象不可以被更改。Java语言还有另两种封装字符串的标准类,即StringBuffer类和StringBuilder类,且其对象可被直接更改。StringBuffer对象对多线程的应用是安全的,而StringBuilder对象并不是如此。4.4.1建立StringBuffer对象StringBuffer aString = new StringBuffer("A switch in time");String phrase = "Experience is what you get when you're expecting something else.";StringBuffer buffer = new StringBuffer(phrase);4.4.2 StringBuffer对象的容量String对象包含固定的字符串,内存是固定的。StringBuffer对象包含了缓冲区,缓冲区的长度被称为StringBuffer对象的容量。StringBuffer aString = new StringBuffer("A switch in time");int theLength = aString.length();int theCapacity = aString.capacity();4.4.3 为StringBuffer对象改变字符串的长度