1.数组:
声明:
方式一:数据元素类型[]数据名;
方式二:数据元素类型 数据名[];
数据声明时,Java并不给数组分配任何空间,仅仅创建了一个引用数组的存储地址([I@15db9742)
数据声明的目的只是告诉操作系统一个新的数组的名称个类型,数组本身不能存放任何数组元素,现阶段的数组值为null,因此,使用数组之前,需要先试用new关键字创建数组,为数组分配指定长度的内存空间。
创建:
数组名 = new 数组元素的类型[数组的长度];
初始化:
若不对其进行初始化,按照类型给数组成员赋默认值
初始化的写法:
double[] score = {1,2,3};
代码:
package Example_6;
import java.io.*;
public class Example_6_2 {
public static void main(String[] args) throws IOException{
int k,count = 5;//count为学生个数
double score[] = new double[count]; //学生的成绩数组
String str;
boolean contiGo=true;
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
for(k=0;k<5;k++){
while(contiGo){
System.out.println("请输入第"+(k+1)+"个学生的成绩");
str = buf.readLine();
score[k] = Double.parseDouble(str);
if(0>score[k]||100<score[k]){
System.out.println("应该介于0到100之间");
}
else break;
}
}
//采用冒泡法,对成绩进行排序
double[] scoreCopy = new double[count+1];
double temp =0;
System.arraycopy(score,0,scoreCopy,1,score.length); //赋值给另外一个数组
for(k=1;k<count;k++)
for(int m=1;m<=count-k;m++)
if(scoreCopy[m]>scoreCopy[m+1])
{
temp = scoreCopy[m];
scoreCopy[m]=scoreCopy[m+1];
scoreCopy[m+1] = temp ;
}
System.out.println("这"+score.length+"个同学的成绩如下:");
for(int j=0;j<score.length;j++)
System.out.print(score[j]+"\t");
System.out.println("\n这"+score.length+"个同学的成绩从低到高排序如下:");
for(int j=1;j<scoreCopy.length;j++)
System.out.print(scoreCopy[j]+"\t");
>
}
}
注意:
int []a = new int[3];
int []b;
b=a; //a,b指向同一块内存区域,若改变其中一个的数组元素的值,对应着另外一个的数组元素的值也会改变
System.arraycopy(a,0,b,0,a.length) //将a的值复制一份给b,二者指向不同的内存区域,改变其中一个另外一个不变.
数组类Arrays:
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。具有以下功能:
给数组赋值:通过fill方法。
对数组排序:通过sort方法,按升序。
比较数组:通过equals方法比较数组中元素值是否相等。
查找数组元素:通过binarySearch方法(有点问题)能对排序好的数组进行二分法查找。
代码:
package Example_6;
import java.util.Arrays;
public class Example_6_5 {
public static void main(String args[]){
int baka[] = new int[11],a[] = {10,100,30,40,50,60,70,80};
System.out.println("a数组的地址为:"+a);
//baka =a;
System.arraycopy(a,0, baka,0, a.length);
Arrays.sort(a);
System.out.println("baka数组的地址为:"+baka);
System.out.println(a[3]);
baka[3]=41;
System.out.println(a[3]);
}
}
2.字符串:
Java中使用String类和StringBuffer类来封装字符串。String定义的变量是不变的(final修饰的字符数组);StringBuffer则是开辟了一块字符缓冲区,其变量可以修改
String的主要方法:
代码:
package Example_6;
public class Example_6_pratice {
public static void main(String[] args) {
String s = new String("HelloWorld");
System.out.println(s);
char[] a = {'a','b','1'}; //字符数组,注意不要丢了单引号
String s1 = new String(a);
// System.out.println(s1);
//字符串类String的主要方法:
System.out.println(s.length());//获得字符串的长度
System.out.println(s.charAt(1)); //截取一个字符
System.out.println(s.substring(3,8)); //取得字串
System.out.println(s.substring(3)); //取得字串
String s2 = "helloworld";
System.out.println(s.equals(s2)); //字符串是否相等比较
System.out.println(s.equalsIgnoreCase(s2)); //字符串是否相等比较,忽略大小写
System.out.println(s.concat(s1)); //字符串连接 concatenate:连接,把 (一系列事件、事情等)联系起来;
System.out.println(s+s1); //记不住也可以用这种方法连接
System.out.println(s.compareTo(s2)); //比较两个字符串的大小,小于则返回一个随机负数,大于返回一个随机正数,相等返回0
System.out.println(s.compareToIgnoreCase(s2)); //忽略大小写比较
System.out.println(s.indexOf('W')); //从第一个位置或者指定位置开始,是否有子串,有则返回其位置,没有返回一个负数
System.out.println(s.indexOf("ell")); //另外使用常量时必须用单引号或者双引号包住
System.out.println(s.indexOf('z'));
s1 ="aaaaaaaaa";
//s1=s1.copyValueOf(a);
System.out.println(String.copyValueOf(a)); //字符数组转化为字符串,(copyValueOf是类方法)
System.out.println(a);
char[] buf = new char[100];
//s1.getChars(1, 3, buf, 0); //字符串转化换为字符数组,前两个参数指定了从字符串哪里截取,第三个是字符数组的名称,最后一个表示放在字符数组的哪里
//buf = s1.toCharArray(); //这种方式也可以
System.out.println(buf);
String s3 = " aaaaaaaaaaaaa ";
System.out.println(s3);
System.out.println(s3.trim()); //去掉首尾的空格
System.out.println(s.replace('e', 'a')); //字符串替换
System.out.println(s.toLowerCase()); //字符串大小写转换
System.out.println(s.toUpperCase());
System.out.println(String.valueOf(100)); //将其他类型转换为字符串,(valueOf的所有重载都是类方法)
System.out.println(String.valueOf(646541.4564));
System.out.println(s.split("W")[0]); //分割字符串,返回字符串数组 split:分裂,分开
}
}
StringBuffer:
代码:
package Example_6;
public class Example_6_pratice_2 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("HelloWorld");//构建一个字符串缓冲区,将其中的内容初始化为指定的字符串内容
// s="aaaaaa"; //这是不允许的
System.out.println(s);
//最重要的两个方法:append和insert。都表示插入,前者只能在最后插入,后者可以在指定的位置插入
System.out.println(s.append("aaa")); //可以append(附加,添加)各种类型的数据
System.out.println(s.append(123));
System.out.println(s.insert(3, "aaa")); //在s[3]开始插入aaa
System.out.println(s.charAt(2)); //s[2],第三个数
System.out.println(s.delete(0,3)); //删除s[0]到s[2]三个字符
System.out.println(s.deleteCharAt(3)); //删除s[3]
char [] ch = new char[30];
s.getChars(0, 4, ch, 0);
System.out.println(ch); //将字符串赋值给字符数组
System.out.println(s.indexOf("123")); //返回第一个"123"出现的索引值(s[12]):12,若没找见返回随机负数
System.out.println(s.indexOf("123",10)); //从10开始寻找,返回索引值
System.out.println(s.lastIndexOf("aaa")); //返回最后一个"aaa"出现的索引值
System.out.println(s.lastIndexOf("aaa",6)); //从6往前数,第一次出现"aaa"的索引值
System.out.println(s.length()); //返回该字符缓冲区中字符串的长度
System.out.println("abc".matches("\\.d")); //返回此字符串是否匹配给定的正则表达式,这个是String的方法,不是StringBuffer的
System.out.println(s.replace(0,s.length(),"saaa")); //将"aaaa"替换 从0开始,s.length()结束的s的字符串
System.out.println(s.reverse()); //将s倒叙存储
s.setCharAt(2, 'b'); //将s[2]设置为b
System.out.println(s);
s.setLength(200); //将缓冲区大小设置为200
System.out.println(s.substring(0,2)); //截取字符串
System.out.println(s.toString());//将StringBuffer转换为String
int a[] = {10,20};
System.out.println(a);
}
}