package case1;
public 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);
}
}
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 hm = new 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);
hm.remove(3);
hm.remove(4, "F");
System.out.print("删除和替换元素后结果为:");
System.out.println(hm);
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[] 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());