包装类、数组、string类浅析及练习

String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2);   //返回true  string s1=‘abc’ string s2=‘abc’ string的值是存储于常量池里边的,当给string赋值的时候回先到常量池寻找是否有相同的值。

String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s3==s4); //返回false

System.out.println(s3.equals(s4))//返回true

基础类型数据存储于栈空间 引用类型数据存储于堆空间 

string s1=‘abc’ string s2=‘abc’ string的值是存储于常量池里边的,当给string赋值的时候回先到常量池寻找是否有相同的值。
equals方法: String里的equals方法重写了object对象里的equals方法
String里的equals方法第一步也是会判断地址值是否相等,相等返回true ,不相等再去判断里面的值是否相等。

str.length 返回整个字符串的长度。
str.trim 去掉字符串两边的空格
str.trim.length 返回整个字符串的长度
str.charAt(); 取出字符串中指定索引位置的字符
str.contains(CharSequence s); 当此字符串包含指定的 char 值时,返回 true。
str.startWith(String s); 判断字符串是否以括号中的字符串开始
str.endWith(String s); 判断字符串是否以括号中的字符串结束
replace(char o, char n); 将指定的字符替换掉指定的字符
replace(CharSequence o, CharSequence n); 将指定的字符串替换掉指定的字符串
split(String s); 将字符串分割成字符串
toUpperCase(); 将字符串转换成大写
toLowerCase(); 将字符串转换成小写
valueOf(any args); 
str.indexOf(String s); 返回指定子字符串在此字符串中第一次出现处的索引。
str.lastIndexOf(String s);返回指定子字符串在此字符串中最后一次出现处的索引。
str.substring(int i); 返回一个新的字符串,它是此字符串的一个子字符串
str.substring(int a, int b); str.substring( int beginIndex, int endIndex)

package com.test;

public class lianxi {
	public static void main(String[] args) {
		String str = "像勇士这样的球队,只有防守一松懈,他们才能抓住机会,"
				+ "打完了三场,爵士还是没找到应对勇士的办法";
		//"球队","机会"
		System.out.println(str.indexOf("球队"));
		System.out.println(str.indexOf("机会"));
		System.out.println(str.lastIndexOf("勇士"));
		
		int m = str.indexOf("球队") + str.indexOf("机会") + str.lastIndexOf("勇士");
		System.out.println(m);
		System.out.println((char)m);
		System.out.println(str.split(",")[4]);
		
		String[] newstr = str.split(""); //用空字符串将这一段话分割成多个以每一个字为一个字符串的数组。
		String temp = "";
		for (int i = 0; i < newstr.length; i++) {
			if(newstr[i].equals("勇")) {
				newstr[i] = "爵";
			} else if(newstr[i].equals("爵")) {
				newstr[i] = "勇";
			}
			temp+=newstr[i];
		}
		System.out.println(temp);
		//勇士抓住机会,找到应对办法
		System.out.print(str.substring(str.indexOf("勇士"),str.indexOf("勇士")+2));
		System.out.print(str.substring(str.indexOf("抓住机会"),str.indexOf("抓住机会")+4));
		System.out.print(str.substring(str.indexOf(","),str.indexOf(",")+1));
		System.out.print(str.substring(str.indexOf("找到应对"),str.indexOf("找到应对")+4));
		System.out.print(str.substring(str.indexOf("办法"),str.indexOf("办法")+2));
		System.out.println();
		
		String qqEmail = "123@qq.com";
		System.out.println(qqEmail.substring(0,qqEmail.indexOf("@")));

  

//增强for循环
public class ForArray
{
        public static void main(String[] args)
        {
                int[] arr = {1,2,3,4,5,6,7};

                for(int a:arr)
                {
                        System.out.print(a);
                }
        }
}
/*
        for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体
        例子中,
        1.arr就是为 要被遍历的对象,可以是数组,集合
        2.循环变量类型就是这个arr数组的数据类型,即int
        3.循环变量名称就是a,这个可以自己定义
*/

  

包装类:
Integer.parseInt();

byte---Byte
short---Short
int---Integer
long---Long

float---Float
double---Double

boolean---Boolean

char---Character

System.out.println(Integer.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);

System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);

//空心正方形(进入for循环先判断i的值,如果i的值在1~7之间而且j=0或者7的时候,那么打印出除了第一行与最后一行的※,当i=1或7的时候第一行跟最后一行的※都打印出来)
/*for( int i=0; i<8; i++){
	if(i>0&&i<7){   //先判断 i的值是否在1-7这个区间,
		for(int j=0;j<8;j++){
			if(j==0||j==7){
				System.out.print("*"+" ");}
			else{
				System.out.print(" "+" ");}
		}
	}else{
		for( int l=0; l<8;l++){
			System.out.print("*"+" ");
		}
	}System.out.println();
}*/

//输出空心菱形。
for( int i=4; i<7; i++){
	for( int j=8;j>0;j--){
		if(j==i||j==8-i){   
			System.out.print("*");
		}else{
			System.out.print(" ");
		}
	}System.out.println();
}
for( int i=1; i<5; i++){
	for( int j=8;j>0;j--){
		if(j==i||j==8-i){
			System.out.print("*");
		}else{
			System.out.print(" ");
		}
	}System.out.println();
}

	//实心菱形
for( int i=4; i>0; i--){    //菱形的上半部分是随着i值得变动输出的*越来越多,所以i值应该是自减
	for( int j=0;j<8;j++){    
		if(j>i&&j<8-i){
			System.out.print("*");
		}else{
			System.out.print(" ");
		}
		}System.out.println();
	}
for( int i=0; i<4; i++){
	for( int j=0;j<8;j++){
		if(j>i&&j<8-i){
			System.out.print("*");
		}else{
			System.out.print(" ");
		}
		}System.out.println();
	}

}

  

原文地址:https://www.cnblogs.com/gaofangquan/p/7184104.html