Java教程 第22页

Java 实例 – 只读集合-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 只读集合

Java 实例 – 只读集合 Java 实例 以下实例演示了如何使用 Collection 类的 Collections.unmodifiableList() 方法来设置集合为只读: /* author by w3cschool.cc Main.java */ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] argv) throws Exception { List stuff = Arrays.asList(new String[] { "a", "b" }); List list = new ArrayList(stuff); list = Collections.unmodifiableList(list); try { list.set(0, "new value"); } catch (UnsupportedOperationException e) { } Set set = new HashSet(stuff); set = Collections.unmodifiableSet(set); Map map = new HashMap(); map = Collections.unmodifiableMap(map); System.out.println("集合现在是只读"); } } 以上代码运行输出结果为: 集合现在是只读 Java 实例

Java 实例 – 链表元素查找-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 链表元素查找

Java 实例 – 链表元素查找 Java 实例 以下实例演示了使用 linkedlistname.indexof(element) 和 linkedlistname.Lastindexof(elementname) 方法在链表中获取元素第一次和最后一次出现的位置: /* author by w3cschool.cc Main.java */ import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList lList = new LinkedList(); lList.add("1"); lList.add("2"); lList.add("3"); lList.add("4"); lList.add("5"); lList.add("2"); System.out.println("元素 2 第一次出现的位置:" + lList.indexOf("2")); System.out.println("元素 2 最后一次出现的位置:"+ lList.lastIndexOf("2")); } } 以上代码运行输出结果为: 元素 2 第一次出现的位置:1 元素 2 最后一次出现的位置:5 Java 实例

Java 实例 – 自定义异常-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 自定义异常

Java 实例 – 自定义异常 Java 实例 以下实例演示了通过继承 Exception 来实现自定义异常: /* author by w3cschool.cc TestInput.java */ class WrongInputException extends Exception { WrongInputException(String s) { super(s); } } class Input { void method() throws WrongInputException { throw new WrongInputException("Wrong input"); } } class TestInput { public static void main(String[] args){ try { new Input().method(); } catch(WrongInputException wie) { System.out.println(wie.getMessage()); } } } 以上代码运行输出结果为: Wrong input Java 实例

Java 实例 – 压栈出栈的方法实现字符串反转-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 压栈出栈的方法实现字符串反转

Java 实例 – 压栈出栈的方法实现字符串反转 Java 实例 以下实例演示了使用用户自定义的方法 StringReverserThroughStack() 来实现字符串反转: /* author by w3cschool.cc StringReverserThroughStack.java */ import java.io.IOException; public class StringReverserThroughStack { private String input; private String output; public StringReverserThroughStack(String in) { input = in; } public String doRev() { int stackSize = input.length(); Stack theStack = new Stack(stackSize); for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); theStack.push(ch); } output = ""; while (!theStack.isEmpty()) { char ch = theStack.pop(); output = output + ch; } return output; } public static void main(String[] args) throws IOException { String input = "www.w3cschool.cc"; String output; StringReverserThroughStack theReverser = new StringReverserThroughStack(input); output = theReverser.doRev(); System.out.println("反转前: " + input); System.out.println("反转后: " + output); } class Stack { private int maxSize;...

Java 实例 – 数字求和运算-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 数字求和运算

Java 实例 – 数字求和运算 Java 实例 以下实例演示了使用do…while结构求0~100的整数数字之和: /* author by w3cschool.cc Main.java */ public class Main { public static void main(String[] args) { int limit=100; int sum=0; int i=1; do { sum=sum+i; i++; } while(i<=limit); System.out.println("sum="+sum); } } 以上代码运行输出结果为: sum=5050 Java 实例

Java 实例 – 获取向量元素的索引值-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 获取向量元素的索引值

Java 实例 – 获取向量元素的索引值 Java 实例 以下实例演示了使用 Collections 类的 sort() 方法对向量进行排序并使用 binarySearch() 方法来获取向量元素的索引值: /* author by w3cschool.cc Main.java */ import java.util.Collections; import java.util.Vector; public class Main { public static void main(String[] args) { Vector v = new Vector(); v.add("X"); v.add("M"); v.add("D"); v.add("A"); v.add("O"); Collections.sort(v); System.out.println(v); int index = Collections.binarySearch(v, "D"); System.out.println("元素索引值为 : " + index); } } 以上代码运行输出结果为: [A, D, M, O, X] 元素索引值为 : 1 Java 实例

Java 实例 – 重载方法异常处理-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 重载方法异常处理

Java 实例 – 重载方法异常处理 Java 实例 以下实例演示了重载方法的异常处理: /* author by w3cschool.cc Main.java */ public class Main { double method(int i) throws Exception{ return i/0; } boolean method(boolean b) { return !b; } static double method(int x, double y) throws Exception { return x + y ; } static double method(double x, double y) { return x + y - 3; } public static void main(String[] args) { Main mn = new Main(); try{ System.out.println(method(10, 20.0)); System.out.println(method(10.0, 20)); System.out.println(method(10.0, 20.0)); System.out.println(mn.method(10)); } catch (Exception ex){ System.out.println("exception occoure: "+ ex); } } } 以上代码运行输出结果为: 30.0 27.0 27.0 exception occoure: java.lang.ArithmeticException: / by zero Java 实例

Java 实例 – 栈的实现-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 栈的实现

Java 实例 – 栈的实现 Java 实例 以下实例演示了用户如何通过创建用于插入元素的自定义函数 push() 方法和用于弹出元素的 pop() 方法来实现栈: /* author by w3cschool.cc MyStack.java */ public class MyStack { private int maxSize; private long[] stackArray; private int top; public MyStack(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; } public void push(long j) { stackArray[++top] = j; } public long pop() { return stackArray[top--]; } public long peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } public static void main(String[] args) { MyStack theStack = new MyStack(10); theStack.push(10); theStack.push(20); theStack.push(30); theStack.push(40); theStack.push(50); while (!theStack.isEmpty()) { long value = theStack.pop(); System.out.print(value); System.out.print(" "); } System.out.println(""); } } 以上代码运行输出结果为: 50...

Java 实例 – 链试异常-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 链试异常

Java 实例 – 链试异常 Java 实例 以下实例演示了使用多个 catch 来处理链试异常: /* author by w3cschool.cc Main.java */ public class Main{ public static void main (String args[])throws Exception { int n=20,result=0; try{ result=n/0; System.out.println("结果为"+result); } catch(ArithmeticException ex){ System.out.println("发算术异常: "+ex); try { throw new NumberFormatException(); } catch(NumberFormatException ex1) { System.out.println("手动抛出链试异常 : "+ex1); } } } } 以上代码运行输出结果为: 发算术异常: java.lang.ArithmeticException: / by zero 手动抛出链试异常 : java.lang.NumberFormatException Java 实例

Java 实例 – 使用 catch 处理异常-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

Java 实例 – 使用 catch 处理异常

Java 实例 – 使用 catch 处理异常 Java 实例 以下实例演示了使用 catch 来处理异常的方法: /* author by w3cschool.cc Main.java */ public class Main{ public static void main (String args[]) { int array[]={20,20,40}; int num1=15,num2=10; int result=10; try{ result = num1/num2; System.out.println("结果为 " +result); for(int i =5;i >=0; i--) { System.out.println ("数组的元素值为 " +array[i]); } } catch (Exception e) { System.out.println("触发异常 : "+e); } } } 以上代码运行输出结果为: 结果为 1 触发异常 : java.lang.ArrayIndexOutOfBoundsException: 5 Java 实例