JAVA的心動之旅
Day1 字串String
1.0 字串的特點以及創建一個字串

public class Practice {//構建字串的3+1種方法
public static void main(String[] args) {
//第一種
String one=new String();
System.out.println("輸出的字串為:"+one);
//第二種
char str[]={'A','B','C'};
String two=new String(str);
System.out.println("輸出的字串為:"+two);
//第三種
byte []bytes={97,98,99,100};
String three=new String(bytes);
System.out.println("輸出的字串為:"+three);
//+1種 直接構造 字串常量不可改變
String four="HelloWorld";
System.out.println("輸出的字串為:"+four);
}
}
列印結果:
輸出的字串為:
輸出的字串為:ABC
輸出的字串為:abcd
輸出的字串為:HelloWorld
2.0 字串的常量池

3.0 字串的獲取方法

public class Practice {
public static void main(String[] args) {
String one="Hello";
System.out.println("字串的長度為:"+one.length());
String two="say ".concat(one);
System.out.println("字串為:"+two);
for(int i=0;i<two.length();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 class Practice {
public static void main(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 class Practice {
public static void main(String[] args) {
String one="hellobts";
char a[]=one.toCharArray();
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
byte []bytes=one.getBytes();
for(int i=0;i<bytes.length;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 class Students {
private static int idcounter=0;//每當創建一個物件(new)計數器++
static String room;
private String name;
private int age;
private int id;
public Students() {
this.id=++idcounter;
}
public Students(String name, int age) {
this.name = name;
this.age = age;
this.id=++idcounter;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Practice { public static void main(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 簡單的應用
import java.util.Arrays;
public class Practice {
public static void main(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<num.length;i++)
{
System.out.print(num[i]+" ");
}
}
}
列印結果:
[3, 89, 45, 235, 43, 79]
3 43 45 79 89 235
2.0 字串倒序
import java.util.Arrays;
public class Practice {
public static void main(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類方法(百度)




轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181213.html
標籤:Java
