GuruNet's profileGuru_Net's BlooooogPhotosBlogLists Tools Help

Blog


    February 07

    Java notes

    出于谋生的原因,最近还是静下心来学习Java programming了,贴一些最近自己动手coding过的代码以及经验吧.Java大牛们狠狠的拍吧.(代码都是来自Core Java 2 Volume I,有所改动)--------------------------------------------------------------------------------------------------------------------public class ArrayTest{public static void main(String args[]){int[] smallPrimes = {2,3,5,7,9,11,13};for(int i = 0;i < smallPrimes.length;i++)System.out.println(i+": "+smallPrimes[i]);smallPrimes = new int[]{1,1,1,1,1,1,1};System.out.println("After reinitialize...");for(int i = 0;i < smallPrimes.length;i++)System.out.println(i+": "+smallPrimes[i]);}}/*1.Array的获取大小的方法是length而不是length();2.可以用 A = new int[]{x,x,x,x,x,x}的方式重新初始化一个Array;*/--------------------------------------------------------------------------------------------------------------------public class ArrayCopyTest{public static void main(String args[]){int[] OldArray = {1,2,3,4,5,6};int[] NewArray = {7,8,9,10,11,12};System.out.println("Before Copying...");System.out.println("OldArray is ...");for(int i = 0;i < OldArray.length;i++)System.out.println(i+": "+OldArray[i]);System.out.println("NewArray is ...");for(int i = 0;i < NewArray.length;i++)System.out.println(i+": "+NewArray[i]);System.arraycopy(NewArray,0,OldArray,0,6);System.out.println("After copying...");System.out.println("OldArray is ...");for(int i = 0;i < OldArray.length;i++)System.out.println(i+": "+OldArray[i]);System.out.println("NewArray is ...");for(int i = 0;i < NewArray.length;i++)System.out.println(i+": "+NewArray[i]);}}/*1.System.arraycopy可以在两个Array之间进行Array元素的值的copy;2.Array元素的index从0开始,在遍历Array时应该是使用< sets.length;i++)sets[i] = i + 1;for(int i = 0;i < k;i++){int r = (int)(Math.random() * n);result[i] = sets[r];sets[r] = sets[n-1];n--;}Arrays.sort(result);for(int r:result)System.out.println("I_th in result is "+r);}}/*1.Math.random()与某个int型相乘后可能损失精度,需要用(int)(Math.random()*n)强制转换,Math类属于java.lang.2.foreach形式的loop里面那个是':'而不是';';3.Arrays类属于java.util,里面有些可用于Array(搜索和排序)的方法.*/--------------------------------------------------------------------------------------------------------------------import java.util.*;public class OutStreamTest{public static void main(String args[]){Scanner in = new Scanner(System.in);System.out.println("What's your name?");String name = in.nextLine();System.out.println("OK,your name is "+name);}}/*总结:1.Scanner必须引用自java.util;2.System.in...3.import一行后面必须有';'*/PS:MSN的空间实在是太慢了,又想搬家了...

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://gurunets.spaces.live.com/blog/cns!26589C6386DA9C55!157.trak
    Weblogs that reference this entry
    • None