Java--基础[String]

package test;
import java.util.Scanner;

public class HelloWord {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * int indexof(int ch): 返回字符在此字符串中第一次出现的索引
		 * int indexof(String str): 返回指定字符串在此字符串中第一次出现的索引
		 */
		System.out.println("hello world");
		// hashcode编码后就是对应的内存地址
		Scanner test = new Scanner(System.in);
		int hashcode = test.hashCode();
		System.out.println("test对象内存地址:" + hashcode);
		System.out.println("test对象转换为字符串:" + test.toString());
		
		// 字符串进行判断是输入的字符串还是其它整形hasNextInt()
		Scanner sc = new Scanner(System.in);  
		if(sc.hasNextLine()) {
			String i = sc.nextLine();
			System.out.println(i);			
		}else {
			System.out.println("type error!");
		}
		//字节转换成字符串;new String 默认会调用toString方法;  还有 indexof, substring, length, CharAt等属性
		byte[] arr1 = {97, 98, 99};
		String s1 = new String(arr1);  // 也可以适应 String(arr1, 1, 2); 将arr1字节数组从1索引开始转换2个
		System.out.println(s1);
		
		Scanner sc2 = new Scanner(System.in);
		// 获取信息; nextInt只获取整数,如果后面再有nextLine的话,会直接获取nextInt后的换车换行符
		int i = sc2.nextInt();
		System.out.println(i);			
		String i2 = sc2.nextLine();
		System.out.println(i2); //会导致这里出现空白
		//concat :把字符串拼接; toUpperCase, getBytes, toCharArray, toLowerCase
		char[] arr = {'a', 'b', 'c', 'd'}
		String a = String.valueOf(arr);
		System.out.println(a); //把字符数组转换成字符串
		// replace, trim, compareTo:字符串比较
	}


}








package test;
import java.util.Arrays;

public class te1 {

	public static void main(String[] args) {
		System.out.println("# 字符串反转");
		String sc = "abc";
		StringBuffer sb = new StringBuffer();
		StringBuffer sb2 = sb.append(sc);
		StringBuffer sb3 = sb2.reverse();
		System.out.println(sb3);
		System.out.println(sb3.toString());
		System.out.println(sb3.hashCode());
		
		System.out.println("# 整形数组输出");
		int[] sc1 = { 1, 2, 3};
		System.out.println(sc1.toString());
		System.out.println(sc1.hashCode());
		
		System.out.println("# 字符串数组转字符串");
		String[] ary = {"abc", "123", "45"};
		StringBuffer sb4 = new StringBuffer();
		for(int i = 0; i < ary.length; i++){
			sb4.append(ary[i]);
		}
		String newStr = sb4.toString();
		System.out.println(newStr.toString());
		System.out.println(newStr.hashCode());
		/*
		 * 冒泡排序:轻的上浮,沉的下降。
		 * 两个相邻位置比较,如果前面的元素比后面的元素大就换位置。
		*/
		int[] arr = {24, 69, 10, 57, 13};
		bublleSort(arr);
		print(arr);
		/*
		 * 选择排序:用一个索引位置上的元素,依次与其它索引位置上的元素比较
		 * 小的在前面,大的在后面 
		*/
		selectSort(arr);
		print(arr);
		/*
		 * 二分查找:
		 * 前提:数组是有序的
		 */
		int[] arr2 = {11,22,33,44,55,66};
		System.out.println(getIndex(arr2, 33));
		/*
		 * Arrays的应用
		 */
		int[] arr3 = {33,22,11,44,66,55};
		System.out.println(Arrays.toString(arr3)); 
		Arrays.sort(arr3);
		System.out.println(Arrays.toString(arr3)); 
		
		System.out.println(Integer.toBinaryString(80));	// 二进制
		System.out.println(Integer.toOctalString(80));	// 八进制
		
		// 整型转换成字符串   int ---> string
		int i = 100;
		String s2 = Integer.toString(i);
		// 字符串转换成整型   string ---> int
		String s1 = "100";
		int i2 = Integer.parseInt(s1);
		
	}
	
	public static void bublleSort(int[] arr) {
		for(int i = 0; i < arr.length-1; i++) {
			for(int j = 0; j  < arr.length-1 -i; j++) {
				if( arr[j] > arr[j+1] ) {
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j+1] = temp;
				}
			}
		}
	}
	
	public static void selectSort(int[] arr) {
		for(int i = 0; i < arr.length - 1; i++ ) {
			for(int j = i + 1; j < arr.length; j++) {
				if( arr[i] > arr[j] ) {
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}
	}
	
	public static int getIndex(int[] arr, int value) {
		int min = 0;
		int max = arr.length - 1;
		int mid = (min + max)/2;
		while(arr[mid] != value) {
			if(arr[mid] < value) {
				min = mid +1;
			}else if(arr[mid] > value) {
				max = mid -1;
			}
			mid = (mid + max)/2;
			if(min > max) {
				return -1;
			}
		}
		return mid;
	}
	
	public static void print(int[] arr) {
		for(int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_39325340/article/details/81506335