实验四 Java实用类及接口实验
一、实验学时
4学时
二、实验目的
(一)熟练掌握字符串的合并、拆开等处理操作;
(二)掌握在不同的日期格式之间熟练地实现转换;
(三)熟练掌握集合中元素的增删改查操作;
(四)熟练其它实用类例如Math类和Random类的使用。
三、预习要求
了解字符串类及其使用方法、了解有哪些时间及日期处理类及其所属的方法、了解集合的概念。
四、实验内容
(一)运行下列程序打印其输出结果,掌握字符串如何截取和定位。
class SubStringExample{
public static void main(String[] args) {
String s=“hello Java语言”;
int n1=s.indexOf(‘a’);
int n2=s.indexOf(“a语”);
System.out.println(“n1=”+n1+" n2=“+n2);
char c=s.charAt(2);
String s1=s.substring(6,10);
String s2=s.substring(4,7);
System.out.println(“c=”+c+” s1=“+s1+” s2="+s2);
}
}
(二)使用String类的public String concat(String str)方法可以把调用该方法的字符串与参数指定的字符串连接,把str指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。
(三)编写一个程序,打印系统当前日期并以不同格式化输出。(掌握Date类和SimpleDateFormat类的使用)
(四)补充下列程序,使其完整并运行。
package sample;
import java.util.*;
public class ArrayListTest {
public static void main(String[] args) {
List l = new ArrayList();
l.add(new Integer(1));
l.add(new Integer(4));
l.add(new Integer(3));
l.add(new Integer(2));
Iterator it = l.iterator();
//完善你的代码
//完善你的代码
System.out.println("Element in list is: " + i);
}
}
}
使用泛型改进上面代码并打印结果,理解使用泛型带来的好处
(五)修改下列程序:(1) 使用泛型 (2)使数据倒序排列
import java.util.;
public class TreeSetTest {
public static void main(String[] args) {
Set c = new TreeSet(new MyComparator());
c.add(new Integer(2)); c.add(new Integer(4)); c.add(new Integer(1));
c.add(new Integer(5)); c.add(new Integer(3)); c.add(new Integer(4));
Iterator it = c.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
int j1 = ((Integer)o1).intValue();
int j2 = ((Integer)o2).intValue();
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}
(六)补充下列程序,使其完整并运行。
import java.util.;
public class HashMapExample{
public static void main(String[] args){
//创建了HashMap对象
HashMap hm=new HashMap();
//向HashMap对象中添加内容不同的键值对
hm.put(1,“A”);
hm.put(3,“B”);
hm.put(4,“C”);
hm.put(2,“D”);
hm.put(5,“E”);
System.out.println("添加元素后的结果为: ");
System.out.println(hm);
//移除了HashMap对象中键为3的值
//替换键值4对应的值为F
//打印输出HashMap中的内容
System.out.print("删除和替换元素后结果为:"
System.out.println(hm);
//取出键2对应的值
String s=(String)o;
System.out.println("键2对应的值为:"+s);
}
}
Map类中数据的存放和List类中数据的存放有什么区别?
(七)补充下列程序,使其完整并运行。
public class GenericMe {
//添加代码段处
}
public static void main(String args[]) {
// 创建不同类型的数组,Integer和String类型
Integer[] intArray = { 1, 2, 3, 4, 5 };
String[] stringArray = { "one", "two", "three", "four", "five" };
System.out.println("整型数组元素为:");
genericMethods(intArray); // 输出整型数组
System.out.println("\n字符串型数组元素为:");
genericMethods(stringArray); // 输出字符串型数组
}
}
程序输出结果:
整型数组元素为:
1,2,3,4,5,
字符串型数组元素为:
one,two,three,four,five,
(八)随机产生一个包含10个整数的数组,并求其中的最大值,最小值,以及平均值。(使用Random和Math类中的方法编写程序)
五、实验注意事项
(一)集合转换成任何其他的对象数组,但是不能直接把集合转换成基本数据类型的数组,因为集合只能存放对象;
(二)注意如果在不同的对象上用错方法的话,可能会产生异常;
(三)注意Random包含在java.util包中,程序需要引入该包。
六、思考题
(一) 分别使用String类和StringBuffer类创建两个字符串对象,并将其内容输出打印。
(二)哪些接口存放的数据是有序的?
(三)哪些接口存放的数据可以重复?
(四)下列程序段执行后的结果是( )。
String s=new String(“abcdefg”);
for(int i=0;i<s.length();i+=3){
System.out.print(s.charAt(i));
}
A. adg B. ACEG C. abcdefg D. abcd
(五)下面的程序段执行后输出的结果。
StringBuffer buf=new StringBuffer(“helloqhd”);
buf.insert(5,“!”);
System.out.println(buf.toString());
输出结果: 。
一、实验学时
4学时
二、实验目的
(一)熟练掌握字符串的合并、拆开等处理操作;
(二)掌握在不同的日期格式之间熟练地实现转换;
(三)熟练掌握集合中元素的增删改查操作;
(四)熟练其它实用类例如Math类和Random类的使用。
三、预习要求
了解字符串类及其使用方法、了解有哪些时间及日期处理类及其所属的方法、了解集合的概念。
四、实验内容
(一)运行下列程序打印其输出结果,掌握字符串如何截取和定位。
class SubStringExample{
public static void main(String[] args) {
String s=“hello Java语言”;
int n1=s.indexOf(‘a’);
int n2=s.indexOf(“a语”);
System.out.println(“n1=”+n1+" n2=“+n2);
char c=s.charAt(2);
String s1=s.substring(6,10);
String s2=s.substring(4,7);
System.out.println(“c=”+c+” s1=“+s1+” s2="+s2);
}
}
(二)使用String类的public String concat(String str)方法可以把调用该方法的字符串与参数指定的字符串连接,把str指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。
(三)编写一个程序,打印系统当前日期并以不同格式化输出。(掌握Date类和SimpleDateFormat类的使用)
(四)补充下列程序,使其完整并运行。
package sample;
import java.util.*;
public class ArrayListTest {
public static void main(String[] args) {
List l = new ArrayList();
l.add(new Integer(1));
l.add(new Integer(4));
l.add(new Integer(3));
l.add(new Integer(2));
Iterator it = l.iterator();
//完善你的代码
//完善你的代码
System.out.println("Element in list is: " + i);
}
}
}
使用泛型改进上面代码并打印结果,理解使用泛型带来的好处
(五)修改下列程序:(1) 使用泛型 (2)使数据倒序排列
import java.util.;
public class TreeSetTest {
public static void main(String[] args) {
Set c = new TreeSet(new MyComparator());
c.add(new Integer(2)); c.add(new Integer(4)); c.add(new Integer(1));
c.add(new Integer(5)); c.add(new Integer(3)); c.add(new Integer(4));
Iterator it = c.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
int j1 = ((Integer)o1).intValue();
int j2 = ((Integer)o2).intValue();
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}
(六)补充下列程序,使其完整并运行。
import java.util.;
public class HashMapExample{
public static void main(String[] args){
//创建了HashMap对象
HashMap hm=new HashMap();
//向HashMap对象中添加内容不同的键值对
hm.put(1,“A”);
hm.put(3,“B”);
hm.put(4,“C”);
hm.put(2,“D”);
hm.put(5,“E”);
System.out.println("添加元素后的结果为: ");
System.out.println(hm);
//移除了HashMap对象中键为3的值
//替换键值4对应的值为F
//打印输出HashMap中的内容
System.out.print("删除和替换元素后结果为:"
System.out.println(hm);
//取出键2对应的值
String s=(String)o;
System.out.println("键2对应的值为:"+s);
}
}
Map类中数据的存放和List类中数据的存放有什么区别?
(七)补充下列程序,使其完整并运行。
public class GenericMe {
//添加代码段处
}
public static void main(String args[]) {
// 创建不同类型的数组,Integer和String类型
Integer[] intArray = { 1, 2, 3, 4, 5 };
String[] stringArray = { "one", "two", "three", "four", "five" };
System.out.println("整型数组元素为:");
genericMethods(intArray); // 输出整型数组
System.out.println("\n字符串型数组元素为:");
genericMethods(stringArray); // 输出字符串型数组
}
}
程序输出结果:
整型数组元素为:
1,2,3,4,5,
字符串型数组元素为:
one,two,three,four,five,
(八)随机产生一个包含10个整数的数组,并求其中的最大值,最小值,以及平均值。(使用Random和Math类中的方法编写程序)
五、实验注意事项
(一)集合转换成任何其他的对象数组,但是不能直接把集合转换成基本数据类型的数组,因为集合只能存放对象;
(二)注意如果在不同的对象上用错方法的话,可能会产生异常;
(三)注意Random包含在java.util包中,程序需要引入该包。
六、思考题
(一) 分别使用String类和StringBuffer类创建两个字符串对象,并将其内容输出打印。
(二)哪些接口存放的数据是有序的?
(三)哪些接口存放的数据可以重复?
(四)下列程序段执行后的结果是( )。
String s=new String(“abcdefg”);
for(int i=0;i<s.length();i+=3){
System.out.print(s.charAt(i));
}
A. adg B. ACEG C. abcdefg D. abcd
(五)下面的程序段执行后输出的结果。
StringBuffer buf=new StringBuffer(“helloqhd”);
buf.insert(5,“!”);
System.out.println(buf.toString());
输出结果: 。

被折叠的 条评论
为什么被折叠?



