实验六 字符串处理及基础类库

更新时间:2024-04-23 23:06:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验六 字符串处理及基础类库

一、实验目的

1、 理解并掌握String类、StringBuffer类; 2、 理解并掌握StringTokenizer类

3、 掌握字符串与其他数据类型的转换 4、 掌握Math类的使用。 5、 了解和掌握集合框架类。

6、 掌握Java Application命令行参数的使用

二、实验内容与要求

1.,理解String类的使用

利用下面的关键代码编写一个完整的程序

String s=new String(\//String s=\System.out.println(\

System.out.println(\

public class theString { }

public static void main(String[] args){ }

String s=new String(\); //String s=\ System.out.println(\+s.length());

System.out.println(\+s.substring(11));

System.out.println(\int): \+s.substring(11,15));

2.理解StringBuffer类的使用

利用下面的关键代码编写一个完整的程序

StringBuffer sb=new StringBuffer(\ sb.append(\sb.insert(12,\System.out.println(sb);

System.out.println(sb.charAt(0)); sb.setCharAt(0,''h'');

System.out.println(sb.charAt(0)); System.out.println(sb);

public class theStringBuffer { }

public static void main(String[] args){ }

StringBuffer sb=new StringBuffer(\); sb.append(\); sb.insert(12,\); System.out.println(sb);

System.out.println(sb.charAt(0)); sb.setCharAt(0,'h');

System.out.println(sb.charAt(0)); System.out.println(sb);

3理解集合泛型的使用

1、编写程序练习List集合的基本使用:

1) 创建一个只能容纳String对象名为names的ArrayList集合; 2)按顺序往集合中添加5个字符串对象:“张三”、“李四”、“王五”、“马六”、“赵七”; 3)对集合进行遍历,分别打印集合中的每个元素的位置与内容;

4)首先打印集合的大小,然后删除集合中的第3个元素,并显示删除元素的内容,然后再打印目前集合中第3个元素的内容,并再次打印集合的大小。

import java.util.ArrayList;

public class theArrayList { }

public static void main(String[] args) { }

ArrayList names = new ArrayList(); names.add(\张三 \); names.add(\李四\); names.add(\王五\); names.add(\马六\);

names.add(\赵七\);

for(int i = 0; i

//删除第三个元素 names.remove(2);

System.out.println(\删除后第3个元素为\+names.get(2)); System.out.println(\删除后集合的大小为:\+names.size());

System.out.println(\第\+i+\个元素为:\+names.get(i));

2、编写程序练习Map集合的基本使用:

1)创建一个只能值只能容纳String对象的person的HashMap集合; 2)往集合中添加5个“键-值”对象:id—>”1”、name—>”张三”、sex—>”男”、age—>”25”、love—>”爱学Java”

3)对集合进行遍历,分别打印集合中的每个元素的键与值; 4)首先打印集合的大小,然后删除集合中的键为age的元素,并显示删除元素的内容,并再次打印集合的大小。、

import java.util.HashMap;

public class theMap {

public static void main(String[] args) {

HashMap< String, String> person = new HashMap(); person.put(\, \); person.put(\, \张三\); person.put(\, \男\); person.put(\, \);

person.put(\, \爱学 Java\);

System.out.println(\ + person.get(\)); System.out.println(\+ person.get(\)); System.out.println(\ + person.get(\)); System.out.println(\ + person.get(\)); System.out.println(\ + person.get(\));

System.out.println(\集合的大小为:\ + person.size());

System.out.println(\删除集合中的键为age的元素:\ + person.get(\)); }

}

person.remove(\);

System.out.println(\删除后元素集合为:\);

System.out.println(\ + person.get(\)); System.out.println(\+ person.get(\)); System.out.println(\ + person.get(\)); //System.out.println(\ System.out.println(\ + person.get(\)); System.out.println(\删除后集合的大小为:\ + person.size());

4.理解Math类的使用

利用下面的关键代码编写一个完整的程序

System.out.println (Math.abs (-5.8)); //5.8 System.out.println (Math.ceil (3.2)); //4 System.out.println (Math.floor (3.8)) //3 System.out.println (Math.round (3.8)); //4 System.out.println (Math.round (3.2)); //3 System.out.println (Math.min (3,2)); //2 System.out.println (Math.max (Math.PI,4)); //4 System.out.println (Math.log (7.0)); //1.94591 System.out.println (Math.pow (7,2)); //72 ---- 49

System.out.println (Math.exp (0.4)); //1.49183

System.out.println (Math.IEEEremainder(10.0,3.0)); //返回1 angle = 0.785398; //以弧度为单位的角,π/4

System.out.println (Math.tan (angle)); //返回该角的正切 System.out.println (Math.asin(0.707107)); //返回反余弦 System.out.println (\:2.71828 System.out.println (\πis:\πis:3.14159

System.out.println(Math.random()); //产生0和1(不含1)之间的伪随机数

public class theMaths {

public static void main(String[] args){

System.out.println (\对-5.8取绝对值是:\+Math.abs (-5.8)); //5.8 System.out.println (\对3.2返回最小(最接近负无穷大)浮点值,该值大于等System.out.println (\对3.8返回最大的(最接近正无穷大)double 值,该值System.out.println (\对3.8返回最接近参数的 long:\+Math.round

于该参数,并等于某个整数:\+Math.ceil (3.2)); //4 小于等于参数,并等于某个整数:\+Math.floor (3.8)); //3 (3.8)); //4

System.out.println (\对3.2返回最接近参数的 long:\+Math.round (3.2)); //3

System.out.println (\返回两个 int 值中较小的一个:\+Math.min (3,2)); System.out.println (\返回a和b的较大值:\+Math.max (Math.PI,4)); //4

//2

System.out.println (\返回 double 值的自然对数(底数是 e):\+Math.log

(7.0)); //1.94591 49

System.out.println (\返回欧拉数 0.4的 double 次幂的值:\+Math.exp System.out.println (\按照 IEEE 754 标准的规定,对参数10.0、3.0进行余

(0.4)); //1.49183

System.out.println (\返回7的2次幂的值:\+Math.pow (7,2)); //72 ----

数运算:\+Math.IEEEremainder(10.0,3.0)); //返回1 }

double angle = 0.785398; //以弧度为单位的角,π/4

System.out.println (\返回角的三角angle的正切:\+Math.tan (angle));

//返回该角的正切

System.out.println (\返回0.707107的反正弦:\+Math.asin(0.707107)); }

System.out.println (\+ Math.E); // e is:2.71828 System.out.println (\+Math.PI); //π is:3.14159

System.out.println(\返回带正号的 double 值:\+Math.random()); //产

//返回反余弦

生0和1(不含1)之间的伪随机数

三、思考题

1.算术运算应使用什么类?

2.Java中如何表示字符串?

3.Java Application如何定义和使用命令行参数?

本文来源:https://www.bwwdw.com/article/gu8p.html

Top