字符串课件中的动手动脑

---恢复内容开始---

 

package ja;//信1605-3 20163432

public class StringPool {

public static void main(String args[])
{

String s0="Hello";

String s1="Hello";

String s2="He"+"llo";//+号起连接作用
//判断字符串是否相等

System.out.println(s0==s1);//true

System.out.println(s0==s2);//true

System.out.println(new String("Hello")==new String("Hello"));//false

}


}

分析总结:Java中内容相同的字符串常量只保存一份以节约内存,所以s0,s1,s2实际上引用的同一个对象

System.out.println(new String("Hello")==new String("Hello"));//false此行代码是比价新建的两个存放hello字符串的存储地址是否相同,

由于两个地址不同故输出为false .

String.equals()方法的实现代码

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) { 
char v1[] = value;
char v2[] = anotherString.value; 
int i = offset;
int j = anotherString.offset; 
while (n-- != 0) { 
if (v1[i++] != v2[j++]) 
return false;
} //???
return true;
}
}
return false;
}

 

public class MyCounter {// 信1605-3 20163432 张运涛

 

    int counter=0;

    public MyCounter(int i) {

        counter=i;

    }

    public  MyCounter increase(int add) {

        counter=counter+add;

        return this;

    }

    public MyCounter decrease(int minus) {

        counter=counter-minus;

        return this;

    }

   

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        MyCounter counter1=new MyCounter(1);

        MyCounter counter2=counter1.increase(100).decrease(2).increase(3);

        System.out.println("计算结果为:"+counter2.counter);

 

    }

 

}

**************************************************************************************************************

字符串加密

源程序设计思想:

输入一个字符串,判断

源程序:

package jaa;//信 1605-3 20163432 张运涛

import java.util.Scanner;
public class T {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

System.out.println("请输入要加密的字串:");

String str="";

String str1="";

int n=0;

str=in.nextLine();

n=str.length();

char temp = 0;

for(int i=0;i<n;i++)

{

if((str.charAt(i) > 64 && str.charAt(i) < 88)||(str.charAt(i) > 96 && str.charAt(i) < 120))

temp=(char) (str.charAt(i) + 3);

else if((str.charAt(i) > 87 && str.charAt(i) < 91)||(str.charAt(i) > 119 && str.charAt(i) < 123))

temp=(char) (str.charAt(i) - 23);

str1+=temp;

}

System.out.println("加密后的字串是:\n"+str1);

}

}

 程序流程图

 

源程序截图:

 

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

length():public int length()//求字符串长度

         String s=”dwfsdfwfsadf”;

         System.out.println(s.length());

charAt():public charAt(int index)//index 是字符下标,返回字符串中指定位置的字符

        String s=”Hello”;

        System.out.println(s.charAt(3));

getChars():public int getChars()//将字符从此字符串复制到目标字符数组

        String str = "abcdefghikl";

        Char[] ch = new char[8];

        str.getChars(2,5,ch,0);

replace():public int replace()//替换字符串

        String s=”\\\”;

        System.out.println(s.replace(“\\\”,”///”));

        结果///;

toUpperase():public String toUpperCase()//将字符串全部转换成大写

         System.out.println(new String(“hello”).toUpperCase());

toLowerCse():public String toLowerCase()//将字符串全部转换成小写

         System.out.println(new String(“HELLO”).toLowerCase());

trim():public String trim()

         String x=”ax  c”;

         System.out.println(x.trim());//是去两边空格的方法

toCharArray(): String x=new String(“abcd”);// 将字符串对象中的字符转换为一个字符数组

           char myChar[]=x.toCharArray();

          System.out.println(“myChar[1]”+myChar[1])

 

---恢复内容结束---

posted @ 2017-10-27 15:24  小张在搬砖  阅读(170)  评论(0编辑  收藏  举报