[Java2入门经典]第4章 数组和字符串

什么是数组,如何声明和初始化数组。
如何访问数组中的单个元素。
如何使用数组中的单个元素。
如何声明嵌套数组。
如何创建可变长度的嵌套数组。
如何创建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对象改变字符串的长度







原文地址:https://www.cnblogs.com/jimwind/p/3248006.html