0
点赞
收藏
分享

微信扫一扫

Java实用类及接口实验

古得曼_63b6 2022-05-05 阅读 67
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);
    }
}
package case2;

public class ConcatDemo {
    public static void main(String[] args) {
        String s1="格局";
        String s2="要大一点";
        s1=s1.concat(s2);
        System.out.println(s1);
    }
}
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());
        }
    }
}
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);
	}
}
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); // 输出字符串型数组
    }
}
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);
    }
}
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);
    }
}
String  s=new String("abcdefg");

for(int i=0;i<s.length();i+=3){

    System.out.print(s.charAt(i));

}  
 StringBuffer buf=new StringBuffer("helloqhd");

        buf.insert(5,"!");

        System.out.println(buf.toString());
举报

相关推荐

0 条评论