冒泡排序

冒泡排序:
冒泡排序的思想:亮亮比较,大的往后放,第一次比完,最大值出现在最大索引值,以此进行这样的比较
最终版本:
public class ArrayText {
public static void main(String[] args) {
int []arr={26,256,56,23,23,63};
BubbleSort(arr) ;
System.out.println("排序后:");
printArray(arr) ;
}
public static void BubbleSort (int[]arr){
for(int x=0;x<arr.length;x++){
for(int y=0;y<arr.length-1-x;y++){
if(arr[y]>arr[y+1]){
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
public static void printArray(int[]arr){  
       System.out.print("[");
       for(int x=0;x<arr.length;x++){   //判断是否为最大索引
       if(x==arr.length-1){
      System.out.println(arr[x]+"]");
       }else{
      System.out.print(arr[x]+",");
       }
       }
}
}
输出结果: 排序后:
[23,23,26,56,63,256]


String:键盘录入一个字符串,将该字符串第一个字母转换成小写,其余字母字符转换成大写,不考虑其他字母字符.


举例:”Helloworld”    H-h  elloworld-ELLOWORLD
流程:1.创建键盘录入对象,录入一个字符串.
     2.截取:substring(int begin,int end)
     3.将上面获取的字符串转换小写
     4.在截取刚才录入的字符串,substring(int begin):从指定位置截取到末尾,返回一个新的字符串
     5.将得到的新的字符串--转换大写
     6.将两个字符串,使用concat()方法拼接.
     
import java.util.Scanner;
public class StringText {
   public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
String s1=line.substring(0,1);
String s2=line.substring(1);
String s3=s1.toLowerCase();
String s4=s2.toUpperCase();
String result=s3.concat(s4);
System.out.println("result:"+result);
//链式编程
String result2=
line.substring(0,1).toLowerCase().concat(line.substring(1).toUpperCase()) ;
}
}


replaceAll(String regex,String replacement):正则表达式.
使用给定的replacement替换给定的正则表达式(eg:游戏骂人替换)


替换功能:
 public String replace(char oldChar,char newChar):
将字符串中某一个字符用新的字符替换
 public String replace(String oldStr,String newStr):
将字符串中某一个子字符串用新 的字符串去替代


去除字符串两端空格:
Public String trim()
两个字符串进行比较:
Public int compareTo(String anotherString) 是Comparable接口的方法
(该接口可以实现一个自然排序)     
按照字典顺序排列    通过Compareable里的compareTo方法.


Compaer(T 01,T 02):比较两个排序.
compaerTo:当两个字符串长度相同时,按顺序比较同位的字符,返回差值.
当长度不同
例子:
public class StringText1 {
public static void main(String[] args) {
String s1="helloworld";
// public String replace(char oldChar,char newChar):将字符串中某一个字符用新的字符替换
String s2=s1.replace('o', 'i');
    System.out.println("S2:"+s2);
    // public String replace(String oldStr,String newStr):将字符串中某一个子字符串用新 的字符串去替代
    String s3=s1.replaceFirst("llo", "Rng");
    System.out.println("s3:"+s3);
    System.out.println("-----------");
    String s4="  nihao   ";
    String s5 = s4.trim() ;
    System.out.println("s5:"+s5);
  //public int compareTo(String anotherString)
    String s6="hello";
    String s7="hello";
    String s8="abc";
    System.out.println("compareTo:" +s6.compareTo(s7));
    System.out.println("compareTo:"  +s6.compareTo(s8));
    }
}


输出结果: 
S2:helliwirld
s3:heRngworld
-----------
s5:nihao
compareTo:0
compareTo:7


eg:
public class StringText2 {
public static void main(String[] args) {
String s1="hello";
String s2="hel";
System.out.println("compareTo():"+s1.compareTo(s2));
}
}
比较的差值 输出结果为2


要求:输入一个字符串,将字符串反转
需求1.键盘录入一个字符串,将字符串进行反转.
2.定义一个空的字符串
3.可以将字符串转换成字符数组:toCharArray();
4.遍历字符数组,倒着遍历
5.遍历之后获取到每个字符串的每一个字符,然后使用空串进行拼接
6.输出即可.


Eg:
import java.util.Scanner;
public class StringText3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串");
String line=sc.nextLine();
String result="";
char[]chs=line.toCharArray();
for(int x=chs.length-1;x>=0;x--){
result+=chs[x];
}
System.out.println("result:"+result);
}
}




线程:
线程安全-同步-执行效率低!
举例:   银行的网站,医院的平台
线程不安全-不同步-执行效率高
举例:   一些论坛网站,相亲网站..
在是开发中,线程不安全,线程不安全可能会造成死锁的现象!
线程安全和执行效率是相对的,并且也是困扰开发者的因素!


StringBuffer:线程安全的可变字符序列.
面试题:
StringBuffer和String 的区别?
StringBuffer会构造一个字符串缓冲区,从内存考虑,一般情况使用StringBuffer比较多
(在单线程程序中使用StringBulider替代)
StringBuffer:线程不安全,单线程单纯为了提供执行效率!
String:普通的一个字符串,从内存角度考虑,耗费空间!


StringBuffer的构造方法:
Public StringBuffer():构造一个不带字符的字符串缓冲区,其初始容量为16个字符
Public StringBuffer(int capacity)构造一个不带字符,但具有指定初始容量的字符串缓冲区.
Public StringBuffer(String str)
构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容.该字符串的初始容量为16


常用的功能:
Public int length():获取字符串长度数.
Public int capacity():获取当前字符串缓冲区的容量.


public class StringBufferDemo {
public static void main(String[] args) {
//public StringBuffer ()构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符
//创建一个字符串缓冲区对象
StringBuffer sb = new StringBuffer() ;
System.out.println("sb.length:"+sb.length());
System.out.println("sb.capacity:"+sb.capacity());//16
System.out.println("-------");
// public StringBuffer(int capacity)构造一个不带字符,但具有指定初始容量的字符串缓冲区
StringBuffer sb2 = new StringBuffer(50) ;
System.out.println("sb2.length:"+sb2.length());
System.out.println("sb2.capacity:"+sb2.capacity());//50
    System.out.println("-----------");
    StringBuffer sb3 = new StringBuffer("hello") ;
System.out.println("sb3.length:"+sb3.length());
System.out.println("sb3.capacity:"+sb3.capacity());//默认:初始容量16 + 当前字符串长度
}
}


输出结果: sb.length:0
sb.capacity:16
-------
sb2.length:0
sb2.capacity:50
-----------
sb3.length:5
sb3.capacity:21


StringBuffer中和添加有关的方法
public StringBuffer append(int/String/char/boolean/double/float....):当前这个方法追加,给缓冲中追加数据,返回字符串缓冲区本身(经常使用的)
public StringBuffer insert(int offset,String str):在某一个位置处去插入str这个字符串,返回字符串缓冲区本身


eg:
ublic class StringText4 {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
// sb.append("hello");
// sb.append(false);
// sb.append(13.45);
//链式编程
sb.append("hello").append(false).append(13.45);
sb.insert(5, "world");//在某一个位置去插入
System.out.println("sb:"+sb);

}
}
输出结果: sb:helloworldfalse13.45


StringBuffer的删除功能:
public StringBuffer deleteCharAt(int index):删除指定位置处的字符,返回的是字符串缓冲区本身!
public StringBuffer delete(int start,int end):删除从指定位置开始到指定位置结束的字符,返回的是字符串缓冲区本身!


public class StringBuffer2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer() ;
sb.append("hello");
sb.append("world");
sb.append("javase");
//需求:要删除当前缓冲区中'e'这个字符
    sb.deleteCharAt(1) ;
// public StringBuffer delete(int start,int end)
//  删除从指定位置到指定结束为止的索引
    sb.delete(3, 8) ;
    System.out.println("sb:"+sb);
}
}


输出结果:sb:hlldjavase


StringBuffer的反转功能:将此字符串中的字符序列直接反转
import java.util.Scanner;
public class StringBuffer5 {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
//录入并接收数据
System.out.println("请您输入一个字符串:");
String line = sc.nextLine() ;
//可以创建字符串缓冲区对象
StringBuffer sb = new StringBuffer(line) ;
//public String toString():现在的数据类型StirngBuffer类型,需要将StringBuffer类型转换String
String result = sb.reverse().toString() ;
System.out.println("result:"+result);
}
}




StringBuffer的替换功能:
Public StringBuffer replace(int start ,int end ,String str)
从指定位置开始到指定位置结束的字符用str子字符串去替代
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
sb.append("nidemingzi");
sb.append("qiushui");
sb.append("sanye");
//public StringBuffer replace(int start, int end,String str)
    //从指定位置开始到指定位置结束的字符串用str子字符串去替代
sb.replace(3, 6, "龙哥");
System.out.println("sb:"+sb);
}




StringBuffer的截取功能:
public String substring(int start):从指定位置默认截取到末尾,返回值是一个新的字符串
public String substring(int start,int end):从指定位置开始截取到指定位置结束,包前不包后,返回一个新的字符串


public static void main(String[] args) {
StringBuffer sb = new StringBuffer() ;
sb.append("hello") ;
sb.append("world") ;
sb.append("java") ;
String s1 = sb.substring(5) ;
System.out.println("s1:"+s1);
String s2 = sb.substring(5,10) ;
System.out.println("s2:"+s2);
}


面试题:
String ,StringBuffer,StringBuilder的区别?
String:一个不可变的字符序列,StringBuffer:和StringBuilder是可变的字符序列,在单个线程中(优选采用StringBuilder)
从线程角度考虑:
StringBuilder线程最不安全,不同步,执行效率高!它比StringBuffer要快


String和StringBuffer作为形式参数
String类型作为形式参数和基本数据类型作为形式参数的效果一样


试题:
public static void main(String[] args) {

//定义两个字符串
String s1 = "hello" ;
String s2 = "world" ;
System.out.println(s1+"---"+s2);
change(s1,s2) ;
System.out.println(s1+"---"+s2);
//定义两个字符串缓冲区对象
StringBuffer sb1 = new StringBuffer("hello") ;
StringBuffer sb2 = new StringBuffer("world") ;
System.out.println(sb1+"---"+sb2);//hello---world
change(sb1,sb2);
System.out.println(sb1+"---"+sb2);//hello---worldworld
}

public static void change(StringBuffer sb1 ,StringBuffer sb2){
sb1 = sb2 ;  
sb2.append(sb1) ;

public static void change(String s1,String s2){
s1 = s2 ;
s2 = s1 + s2 ;
}
}
String类型经过change方法改变后:值不发生改变.
StringBuffer在change方法改变后
首先输出的sb1值不发生变化.但是在缓冲区成为”world”
再用sb2拼接sb1.最终输出的结果为hello---worldworld


输出结果为: hello---world
hello---world
hello---world
hello---worldworld


类与类之间的转换:
A类型-B类型
里面可能最终使用B类型里面的功能
B类型-A类型
有时候需要的不是B类型,所以又需要转换


StringBuffer-String之间的相互转换


面试题:StringBuffer和数组的区别
共同特点:都属于容器类型的变量
数组:只能存储同一种数据类型的元素,数组的长度是固定的.
Eg: int[] arr = {10,20,40,50,"hello"}  错误的
StringBuffer:字符串缓冲区,可以存储任意类型的元素,可以不断的去给缓冲区中追加(append)
字符串缓冲区中:在内存始终返回的字符串


public class StringBuffer7 {
public static void main(String[] args) {
//String--->StringBuffer
String s="hello";
//方式1)使用StringBuffer的构造方式:StringBuffer(String str)
    StringBuffer sb=new StringBuffer(s);
    System.out.println("sb:"+sb);
  //方式2)创建字符串缓冲区对象,使用无参构造StringBuffer(),利用append()
    StringBuffer sb2 = new StringBuffer() ;
sb2.append(s) ;
System.out.println("sb2:"+sb2);
//StringBuffer--->String
StringBuffer buffer = new StringBuffer("world") ;
//String的另一种构造方法:String(StringBuffer buffer)
String s2 = new String(buffer) ;
System.out.println("s2:"+s2);
//public String toString():现在的数据类型StirngBuffer类型,需要将StringBuffer类型转换String
String s3 = buffer.toString() ;
System.out.println("s3:"+s3);
}
}


使用String和StringBuffer实现字符串的反转
import java.util.Scanner;
public class StringBuffer8 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串");
String chs=sc.nextLine();
//使用String类型那个进行拼接
String result = myReverse(chs) ;
System.out.println("result:"+result);
//方式2:使用StringBuffer的reverse()功能
String result2 = myReverse2(chs) ;
System.out.println("result2:"+result2);
}
//StringBuffer的rever()方法实现
public static String myReverse2(String s){
return new StringBuffer(s).reverse().toString() ;
}
//String类型的方式进行反转
public static String myReverse(String s){
String result = "" ;
char[] chs = s.toCharArray() ;
for(int x = chs.length-1 ; x >=0 ; x --){
result += chs[x] ;
}
return result ;
}
}


Integer:需求:求一个整数100对应的二进制,八进制,十六进制
求出Integer类型范围:
Java的jdk5.0之后,出现了新的特性:自动拆装箱,可变参数,增强for循环,静态倒入
枚举
对于每个基本数据类型都会被自动封装成为一个引用类型
基本类型 int      char          byte   boolean     double   float   long  short
引用类型Integer Character      Byte   Boolean     Double  Float   Long
Short


将基本类型转换成引用类型的目的是为了和String类型作为转换


public class IntegerText {
public static void main(String[] args) {
//public static String toBinaryString(int i)
System.out.println(Integer.toBinaryString(100)); //二进制
//public static String toOctalString(int i)
System.out.println(Integer.toOctalString(100)); //八进制
//public static String toHexString(int i)
System.out.println(Integer.toHexString(100));  //十六进制
//public static final int MAX_VALUE
//public static final int MIN_VALUE
System.out.println(Integer.MAX_VALUE);      //最大值
System.out.println(Integer.MIN_VALUE);    //最小值
}
}
输出结果:
1100100
144
64
2147483647
-2147483648


Integer的构造方法:
Public Integer(int value):将一个int类型的数据封装成一个引用类型
Public Integer(String s):讲一个字符数类型封装成一个Integer类型
注意:该字符串必须是数字字符串.   
否则会出现java.lang.NumberFormatException异常
Eg:
public class IntegerText2 {
public static void main(String[] args) {
Integer i=new Integer(100);
Integer y=100;//jdk5.0后的自动装箱
System.out.println("i:" +i);
   // String s="abc";//编译不会报错,
    //运行出现java.lang.NumberFormatException异常
     String s="20";
    Integer i1=new Integer(s);
    System.out.println("s:" +s);
}
}




String类型和int类型的相互转换:
public class IntegerText3 {
public static void main(String[] args) {
//定义一个int类型的数据
int number = 100 ;
//方式1)字符串拼接符
String s1 = "" + number ;
System.out.println("s1:"+s1);
//方式2:int--->Integer类型
Integer i = new Integer(number) ;
//1.Integer--->String //public String toString()
String s2 = i.toString() ;
System.out.println("s2:"+s2);
//方式3)public static String toString(int i)
String s3 = Integer.toString(number) ;
    System.out.println("s3:"+s3);
  //方式4)public static Integer valueOf(int i)
  Integer integer= Integer.valueOf(number) ;
  String s4 = integer.toString() ;
  System.out.println("s4:"+s4);
   }
}


面试题:
Integer i1=127;
Integer i2=127;
System.out.println(i1==i2);//true
Integer i3 = 128 ;
Integer i4 = 128 ;
System.out.println(i3==i4);//false


原因是因为Integer有内存缓冲区:它的范围是
low=-128         High=127;


Character类载对象中包装一个基本类型char的值.Character类型的对象包含类型为char的单个字段
构造方法:
public Character(char value)构造一个新分配的 Character 对象,用以表示指定的 char 值
public static void main(String []args){
Character character =new Character((char)97);
Character character =new Character(“a”);
System.out.println("character:"+character);
}


Character类的判断功能:
public static boolean isLowerCase(char ch)确定指定字符是否为小写字母。
public static boolenn isUpperCase(char ch)确定指定字符是否为大写字母
public static boolean isDigit(char ch)确定指定字符是否为数字。


转换功能:
public static char toUpperCase(char ch):将指定字符转换成大写
public static char toLowerCase(char ch):将指定字符转换成小写
 


例题: :键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符有多少个(不考虑其他字符,使用Character提供的判断功能去完成)


import java.util.Scanner;
public class CharacterText3 {
public static void main(String[] args) {
int bigCount = 0 ;
int smallCount = 0 ;
int numberCount = 0;
Scanner sc= new  Scanner(System.in) ;
System.out.println("请您输入一个字符串:");
String line = sc.nextLine();
char[]  chs = line.toCharArray() ;
for(int x = 0 ; x < chs.length ; x ++){
char ch = chs[x] ;
if(Character.isUpperCase(ch)){
bigCount ++ ;
}else if(Character.isLowerCase(ch)){
smallCount ++ ;
}else if(Character.isDigit(ch)){
numberCount ++ ;
}
}
System.out.println("大写字母字符共有:"+bigCount+"个");
System.out.println("小写字母字符共有:"+smallCount+"个");
System.out.println("数字字符共有:"+numberCount+"个");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值