04747_Java语言程序设计(一)_第4章_数组和字符串

/* 面试题
* 1:String,StringBuffer,StringBuilder的区别?
*
* String是内容不可变的,StringBuffer,StringBuilder都是内容可变的
* StringBuffer是同步的,数据安全,效率低,StringBuilder是不同步的,数据不安全,效率高
*
* 2:StringBuffer和数组的区别?
*
* 二者都可以看成是一个容器,装其他的数据
* 但是StringBuffer的数据最终是一个字符串数据
* 而数组可以放置多种数据,但必须是同一种数据类型的
*
* 3:形式参数问题
* String作为参数传递
* StringBuffer作为参数传递
*
* 形式参数:
* 基本类型:形式参数的改变不影响实际参数
* 引用类型:形式参数的改变直接影响实际参数
*
* 注意:
* String作为参数传递,效果和基本类型作为参数传递是一样的
*/

/* 面试题
 * 1:String,StringBuffer,StringBuilder的区别?
 * 
 * String是内容不可变的,StringBuffer,StringBuilder都是内容可变的
 * StringBuffer是同步的,数据安全,效率低,StringBuilder是不同步的,数据不安全,效率高
 * 
 * 2:StringBuffer和数组的区别?
 * 
 * 二者都可以看成是一个容器,装其他的数据
 * 但是StringBuffer的数据最终是一个字符串数据
 * 而数组可以放置多种数据,但必须是同一种数据类型的
 * 
 * 3:形式参数问题
 * String作为参数传递
 * StringBuffer作为参数传递
 * 
 * 形式参数:
 * 基本类型:形式参数的改变不影响实际参数
 * 引用类型:形式参数的改变直接影响实际参数
 * 
 * 注意:
 * String作为参数传递,效果和基本类型作为参数传递是一样的
 */

public class StringTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		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);
		change(sb1,sb2);
		System.out.println(sb1+"---"+sb2);
	}
	
	private static void change(String s1, String s2) {
		// TODO Auto-generated method stub
		s1=s2;
		s2=s1+s2;
	}
	
	private static void change(StringBuffer sb1, StringBuffer sb2) {
		// TODO Auto-generated method stub
		sb1=sb2;
		sb2.append(sb1);
	}
}

面试题

字符串连接

public class Aserver {
	public static void main(String args[]) {
		// 字符串数据和其他数据+,结果是字符串类型
		// 运算符重载+,字符串拼接
		System.out.println("hello" + 'a' + 1);// helloa1
		System.out.println('a' + 1 + "hello");// 98hello

		System.out.println("5+5=" + 5 + 5);// 5+5=55
		System.out.println(5 + 5 + "=5+5");// 10=5+5
	}
}

模拟用户登录

import java.util.Scanner;

public class StringTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int count = 3;
		String username = "admin";
		String password = "admin";

		for (int i = 0; i < count; i++) {
			Scanner sc = new Scanner(System.in);

			System.out.println("请输入用户名");
			String name = sc.nextLine();

			System.out.println("请输入密码");
			String pwd = sc.nextLine();

			if (name.equals(username) && pwd.equals(password)) {
				System.out.println("登录成功");
				break;
			} else {
				if (i == 2) {
					System.out.println("账号被锁定,请与管理员联系。");
					break;
				}
				System.out.println("登录失败,你还有" + (count - i - 1) + "次机会。");
			}
		}
	}
}

例4.1小应用程序先声明一个数组a,在方法init()中创建它,指定有5个元素,然后为数组元素逐一赋值。在方法paint()中输出数组各元素的值。

import java.applet.*;
import java.awt.*;

public class Example4_1 extends Applet {
	int a[];// 标识符ua能引用元素类型是int的数组

	public void init() {
		a = new int[5];// 创建一个含5个元素的数组,并让a引用它
		a[0] = 100;
		a[1] = 101;
		a[2] = 102;
		a[3] = 103;
		a[4] = 104;
	}

	public void paint(Graphics g) {
		g.drawString("a[0]=" + a[0] + " a[1]=" + a[1] + " a[2]=" + a[2], 12, 12);
		g.drawString("a[3]=" + a[3] + " a[4]=" + a[4], 12, 32);
	}
}

例4.2设明数组是一种引用类型的应用程序。

public class Example4_2 {
	static public void main(String[] args) {
		int firstArray[] = { 1, 2, 3, 4 };// 采用数组初始化创建数组
		int secondArray[] = { 5, 6, 7, 8, 9, 10 };
		int myArray[];// 声明标识符myArray可以引用数组
		myArray = firstArray;// myArray与firstArray一样,引用同一个数组
		System.out.println("First Array:");
		for (int index = 0; index < myArray.length; index++) {
			System.out.println(myArray[index]);// 通过myArray对数组做操作
		}
		myArray = secondArray;// 让myArray引用另一个数组
		System.out.println("Second Array:");
		for (int index = 0; index < myArray.length; index++) {
			System.out.println(myArray[index]);// 通过myArray对数组做操作
		}
	}
}

例4.3一个含三角二维数组的应用程序。

public class Example4_3 {
	static public void main(String[] args) {
		boolean bTbl[][] = new boolean[4][];// 仅说明第一维,有4个子数组
		for (int i = 0; i < bTbl.length; i++)// 创建第二维
		{
			bTbl[i] = new boolean[i + 1];
		}
		for (int i = 0; i < bTbl.length; i++)// 在屏幕上打印数组内容
		{
			for (int k = 0; k < bTbl[i].length; k++) {
				System.out.print(bTbl[i][k] + " ");
			}
			System.out.println("");
		}
	}
}

例4.4一个说明字符串连接运算和连接方法的应用程序。

public class Example4_4 {
	static public void main(String[] args) {
		String s1 = "ABC";
		String s2 = "DEFG";
		System.out.println(s1 + s2);// 应用字符串连接运算
		s1 = "ABC";
		s2 = "XYZ";
		s1 = s1.concat(s2);// 应用字符串连接方法
		System.out.println(s1);
	}
}

例4.5一个说明字符串的字符替换和去掉字符串前后空格的应用程序。

public class Example4_5 {
	static public void main(String[] args) {
		String s = "1234567788", str;
		str = s.replace('7', 'A');
		System.out.println("s=" + s + " str=" + str);
		String s2 = " 123456 77 88 ", str2;
		str2 = s2.trim();
		System.out.println("s2=" + s2 + "| str2=" + str2 + "|");
	}
}

例4.6利用StringTokenizer类分析字符串。

import java.util.*;

public class Example4_6 {
	public static void main(String[] args) {
		String s1 = "public static void, main";
		String s2 = "StringTokenizer pas2? =new StringTokenizer";
		StringTokenizer pas1 = new StringTokenizer(s1, " ,");// 空格逗号做分隔符
		StringTokenizer pas2 = new StringTokenizer(s2, "? ");// 问号空格做分隔符
		int n1 = pas1.countTokens(), n2 = pas2.countTokens();
		System.out.println("s1有单词:" + n1 + "个,全部单词如下:");
		while (pas1.hasMoreTokens()) {
			String s = pas1.nextToken();
			System.out.println(s);
		}
		System.out.println("s2有单词:" + n2 + "个,全部单词如下:");
		while (pas2.hasMoreTokens()) {
			String s = pas2.nextToken();
			System.out.println(s);
		}
	}
}

例4.7一个说明字符串与字节数组的应用程序。

import java.util.*;

public class Example4_7 {
	public static void main(String[] args) {
		byte b[] = new byte[10];
		for (int k = 0; k < 10; k++) {
			b[k] = (byte) (k + 48);// 数字符0的ASCII码值是48
		}
		String s1 = new String(b);
		String s2 = new String(b, 3, 6);
		String s3 = new String("ABC");
		String s4 = new String("Java程序");
		byte c[] = s3.getBytes();
		byte d[] = s4.getBytes();
		System.out.println(s2);
		System.out.println(s3);
		System.out.println("数组c的长度是:" + c.length);
		System.out.println("数组d的长度是(一个汉字占两个字节):" + d.length);
		System.out.println("用ASCII数值输出数组c[]:");
		for (int i = 0; i < c.length; i++) {
			System.out.println("c" + "[" + i + "]:" + c[i]);
		}
		System.out.println("用ASCII数值对应的字符输出数组c[]:");
		for (int i = 0; i < c.length; i++) {
			System.out.println("c" + "[" + i + "]:" + (char) c[i]);
		}
		System.out.println("用ASCII数值输出数组d[]:");
		for (int i = 0; i < d.length; i++) {
			System.out.println("d" + "[" + i + "]:" + d[i]);
		}
	}
}

例4.8程序获取Date对象的小时、分和秒,获得Date对象的字符串表示,并测试一个循环所消耗的时间。

import java.util.*;

public class Example4_8 {
	public static void main(String[] args) {
		Date date1 = new Date();
		System.out.println("日期及时间是:" + date1);
		String t = date1.toString();
		t = t.substring(11, t.indexOf('C'));// 截取时、分、秒部分。
		System.out.println("现在时间是:" + t);
		double sum = 0.0;
		for (double i = 0; i < 80000000; i++) {
			sum = sum + i;
		}
		System.out.println("" + sum);
		Date date2 = new Date();// 循环结束后的时间。
		t = date2.toString();
		t = t.substring(11, t.indexOf('C'));// 字符C之前为时分秒
		System.out.println("现在时间是:" + t);
	}
}
原文地址:https://www.cnblogs.com/denggelin/p/6235102.html