JAVA的心动之旅
Day1 字符串String
1.0 字符串的特点以及创建一个字符串
public class Practice {//构建字符串的3+1种方法
public static voidmain(String[] args) {//第一种
String one=newString();
System.out.println("输出的字符串为:"+one);//第二种
char str[]={'A','B','C'};
String two=newString(str);
System.out.println("输出的字符串为:"+two);//第三种
byte []bytes={97,98,99,100};
String three=newString(bytes);
System.out.println("输出的字符串为:"+three);//+1种 直接构造 字符串常量不可改变
String four="HelloWorld";
System.out.println("输出的字符串为:"+four);
}
}
打印结果:
输出的字符串为:
输出的字符串为:ABC
输出的字符串为:abcd
输出的字符串为:HelloWorld
2.0 字符串的常量池
3.0 字符串的获取方法
public classPractice {public static voidmain(String[] args) {
String one="Hello";
System.out.println("字符串的长度为:"+one.length());
String two="say ".concat(one);
System.out.println("字符串为:"+two);for(int i=0;i
{char ch=two.charAt(i);
System.out.print(ch+" ");
}
String three="o";
System.out.println("\n"+one.indexOf(three));
String four="h";
System.out.println(one.indexOf(four));
}
}
打印结果
字符串的长度为:5
字符串为:say Hello
s a y H e l l o
4
-1
4.0 字符串的比较 ==为地址的比较
5.0 字符串的截取
public classPractice {public static voidmain(String[] args) {
String one="hellobts";
String two=one.substring(5);
System.out.println(two);
String three=one.substring(0, 5);
System.out.println(three);
}
}
打印结果:
bts
hello
6.0 字符串的转化方法
public classPractice {public static voidmain(String[] args) {
String one="hellobts";char a[]=one.toCharArray();for(int i=0;i
{
System.out.print(a[i]+" ");
}
System.out.println();byte []bytes=one.getBytes();for(int i=0;i
{
System.out.print(bytes[i]+" ");
}
System.out.println();
String two=one.replace("hello", "love");
System.out.println(two);
}
}
打印结果:
h e l l o b t s
104 101 108 108 111 98 116 115
lovebts
Day2 static 关键字
1.0 static 概述
一旦用了static 关键字,那么这样的内容不再属于对象自己,它是属于类的,所以凡是本类的对象,都共享一份。
2.0 static 修饰成员变量
public classStudents {private static int idcounter=0;//每当创建一个对象(new)计数器++
staticString room;privateString name;private intage;private intid;publicStudents() {this.id=++idcounter;
}public Students(String name, intage) {this.name =name;this.age =age;this.id=++idcounter;
}public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}
}
public classPractice {public static voidmain(String[] args) {
Students one=new Students("田小娟",22);
one.room="101教室";
Students two=new Students("徐穗珍",22);
System.out.println("姓名:"+one.getName()+" "+"年龄:"+one.getAge()+" 教室:"+one.room+
" 学号"+one.getId());
System.out.println("姓名:"+two.getName()+" "+"年龄:"+two.getAge()+" 教室:"+two.room+
" 学号"+two.getId());
}
}
3.0 static 修饰方法
4.0 静态代码块
Day3 与Arrays相识
1.0 简单的应用
importjava.util.Arrays;public classPractice {public static voidmain(String[] args) {int []num={3,89,45,235,43,79};
String str=Arrays.toString(num);
System.out.println(str);
Arrays.sort(num);for(int i=0;i
{
System.out.print(num[i]+" ");
}
}
}
打印结果:
[3, 89, 45, 235, 43, 79]
3 43 45 79 89 235
2.0 字符串倒序
importjava.util.Arrays;public classPractice {public static voidmain(String[] args) {
String str="aihfjsdfhuwefwnf";//定义一个随机的字符串,并将字符串排序后倒序输出//需将字符串先转化为字符数组,才能使用Arrays
char []chars=str.toCharArray();
Arrays.sort(chars);for(int i=chars.length-1;i>=0;i--)
{
System.out.print(chars[i]+" ");//w w u s n j i h h f f f f e d a
}
}
}
Math类方法(百度)