Java实用类及接口实验

(一)运行下列程序打印其输出结果,掌握字符串如何截取和定位。
package case1;

public class SubStringExample {
    public static void main(String[] args) {
        String s = "hello Java语言";
        // indexOf(String str):返回指定字符str在字符串中(方法调用者)第一次
        // 出现处的起始索引,如果此字符串中没有这样的字符,则返回 -1。
        int n1 = s.indexOf('a');
        int n2 = s.indexOf("a语");
        System.out.println("n1=" + n1 + " n2=" + n2);
        // charAt(int index)方法:返回指定索引位置的char值
        char c = s.charAt(2);
        // substring() 方法:返回字符串的子字符串
        // beginIndex -- 起始索引(包括), 索引从 0 开始
        // endIndex -- 结束索引(不包括)
        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指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。
package case2;

public class ConcatDemo {
    public static void main(String[] args) {
        String s1="格局";
        String s2="要大一点";
        s1=s1.concat(s2);
        System.out.println(s1);
    }
}
(三)编写一个程序,打印系统当前日期并以不同格式化输出。(掌握Date类和SimpleDateFormat类的使用)
package case3;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatDemo {

    public static void main(String[] args) {
        // 获取不同格式化风格和中国环境的日期
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
        DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);
        DateFormat df3 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
        DateFormat df4 = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);

        // 获取不同格式化风格和中国环境的时间
        DateFormat df5 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINA);
        DateFormat df6 = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINA);
        DateFormat df7 = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CHINA);
        DateFormat df8 = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINA);

        // 将不同格式化风格的日期格式化为日期字符串
        String date1 = df1.format(new Date());
        String date2 = df2.format(new Date());
        String date3 = df3.format(new Date());
        String date4 = df4.format(new Date());

        // 将不同格式化风格的时间格式化为时间字符串
        String time1 = df5.format(new Date());
        String time2 = df6.format(new Date());
        String time3 = df7.format(new Date());
        String time4 = df8.format(new Date());

        // 输出日期
        System.out.println("SHORT:" + date1 + " " + time1);
        System.out.println("FULL:" + date2 + " " + time2);
        System.out.println("MEDIUM:" + date3 + " " + time3);
        System.out.println("LONG:" + date4 + " " + time4);
    }
}
(四)补充下列程序,使其完整并运行并使用泛型改进上面代码并打印结果,理解使用泛型带来的好处。
package case4;

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();
        // 完善你的代码
        while (it.hasNext()) {
            // 完善你的代码
            System.out.println("Element in list is: " + it.next());
        }
    }
}
(五)修改下列程序:(1) 使用泛型 (2)使数据倒序排列
package case5;

import java.util.*;

public class TreeSetTest {
    public static void main(String[] args) {
        Set<Integer> 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));
    }
}
(六)补充下列程序,使其完整并运行。
package case6;

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的值
		hm.remove(3);

		// 替换键值4对应的值为F
		hm.remove(4, "F");

		// 打印输出HashMap中的内容
		System.out.print("删除和替换元素后结果为:");
		System.out.println(hm);

		// 取出键2对应的值
		String o = (String) hm.get(2);
		String s = (String) o;
		System.out.println("键2对应的值为:" + s);
	}
}

Map类中数据的存放和List类中数据的存放有什么区别?
Map类:jdk 7以前是以数组和链表结构存储,jdk 8之后以数组、链表和红黑树存储;
List类:以数组形式存储。

(七)补充下列程序,使其完整并运行。
package case7;

public class GenericMe<T> {
    // 添加代码段处
    public void genericMethods(T arr) {
        if (arr instanceof String[]) {
            String[] arr1 = (String[]) arr;
            for (int i = 0; i < arr1.length; i++) {
                System.out.print(arr1[i] + ",");
            }
        }
        if (arr instanceof Integer[]) {
            Integer[] arr2 = (Integer[]) arr;
            for (int i = 0; i < arr2.length; i++) {
                System.out.print(arr2[i] + ",");
            }
        }
    }

    public static void main(String args[]) {
        // 创建不同类型的数组,Integer和String类型
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        String[] stringArray = { "one", "two", "three", "four", "five" };

        GenericMe<Object> tGenericMe = new GenericMe<Object>();

        System.out.println("整型数组元素为:");
        tGenericMe.genericMethods(intArray); // 输出整型数组

        System.out.println("\n字符串型数组元素为:");
        tGenericMe.genericMethods(stringArray); // 输出字符串型数组
    }
}

程序输出结果:
整型数组元素为:1,2,3,4,5,
字符串型数组元素为: one,two,three,four,five,

(八)随机产生一个包含10个整数的数组,并求其中的最大值,最小值,以及平均值。(使用Random和Math类中的方法编写程序)
package case8;

import java.util.Random;

public class Random_number {
    public static void main(String[] args) {
        Random random = new Random();
        int rand = 0;
        int[] arrays = new int[10];
        int max = 0;// 最大值
        int min = 0;// 最小值
        int sum = 0;// 总和
        double average = 0.0;// 平均值

        // 生成数组
        System.out.println("随机生成的10个整数为:");
        for (int i = 0; i < 10; i++) {
            rand = random.nextInt();
            arrays[i] = rand;
            System.out.println(rand);
        }

        // 求总和
        for (int i = 0; i < 10; i++) {
            sum += arrays[i];
        }

        // 求最大值和最小值
        max = arrays[0];
        min = arrays[0];
        for (int i = 0; i < 10; i++) {
            if (arrays[i] > max) {
                max = arrays[i];
            }
            if (arrays[i] < min) {
                min = arrays[i];
            }
        }

        average = sum / 10;// 计算平均值

        System.out.println("总和: " + sum);
        System.out.println("平均值: " + average);
        System.out.println("最大值: " + max);
        System.out.println("最小值: " + min);
    }
}
(九) 分别使用String类和StringBuffer类创建两个字符串对象,并将其内容输出打印。
package case9;

public class String_StringBuffer {
    public static void main(String[] args) {
        String str1 = "string";
        StringBuffer str2 = new StringBuffer("StringBuffer");
        System.out.println(str1);
        System.out.println(str2);
    }
}
(十)哪些接口存放的数据是有序的?

ArrayList、LinkedList、Vector、LinkedMap接口的存放的数据是有序的。但是LinkHashSet、 LinkedHashMap、TreeSet存放的数据是有序的,但是底层存储是无序的。

(十一)哪些接口存放的数据可以重复?

ArrayList、LinkedList、Vector、Collection存放的数据可重复。

(十二)下列程序段执行后的结果是(A)。
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());

输出结果: hello!qhd

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值