1.1 Programming model

读完本文你会了解:

  • Java有关数组的基础知识
  • 静态方法的深入理解-见参考
  • 练习一下二分查找

Primitive types:

data type representation
byte 8-bit
char 16-bit
short 16-bit
int 32-bit
float 32-bit
long 64-bit

声明语句:声明语句将一个变量名和一个类型在编译时关联起来。Java是一种强类型的语言,因为Java编译器会检查类型的一致性。

循环语句:

  • break语句,立即从循环中退出
  • continue语句,立即开始下一循环

Array

Definition:An arroy stores a sequence of values that are all of the same type.if we have N values,we can use the notation a[i] to refer ith value of i from 0 to N-1.

Making an array involves three distinct steps:

  • Declare the array name and type
  • Creat the array
  • Initialize the array values
//颠倒数组的元素
int N = a.length;
for (int i=0; i < N/2; i++) {
    double temp = a[i];
    a[i] = a[N-1-i];
    a[N-1-i] = temp;
}

//矩阵相乘
int N = a.length;
double[][] c = new double[N][N];
for (int i=0;i < N; i++)
    for (int j=0; j < N; j++){
        for (int k=0;k < N; k++)
            c[i][j] += a[i][k]*a[k][j];
    }

example:

double[] a;               //declaration
a = new double[N];        //creation 
for(int i = 0; i<N ; i++) //initialization
    a[i] = 0.0;
  • Using:Java does automatic bounds checking-if you access an array with an illegal index your program will terminate with an ArrayIndexOutOfBoundsException
  • Alising(混淆):An array name refers to the whole array-if wo assign one array name to another,then both refer to the same array,as illustrate in the following code fragment.
int [] a = new int[N];
...
a[i] = 1234;
...
int [] b = a;
...
b[i] = 5678;   //a[i] = 5678.

Static method

  • Defining a static method:A method encapsulates a computation that is defined as a statements. A method takes arguments and computes a return value of some data type or causes a side effect.
  • Java method have features:
    • Arguments are passd by value.
    • Method names can be overloaded.
    • A method has a signal return but may have multiple return statements.
    • A method can have side effects.

Binary Search

public class BinarySearch{
    public static int rank(int key, int[] a){
        int lo = 0;
        int hi = a.length - 1;
        while(lo <= hi){
            int mid = lo + (hi - lo)/2;
            if (key < mid) hi = mid - 1;
            if (key > mid) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

    public static void main(String[] args){
        In in = new In(args[0]);
        int[] whitelist = in.readInts();
        Array.sort(whitelist);
        while(!in.isEmpty()){
            int key = in.readInt();
            if (rank(key, whitelist) < 0)
                StdOut.println(key)
        }
    }
}

Web Exercises

1 Wget. Write a program Wget.java that reads in data from the URL specifiedon the command line and saves it in a file with the same name.

public static void main(String[] args)
{
    String URL = args[0];
    In in = new In(URL);
    String data = Sed.readAll();
    String filename = URL.substring(URL.lastIndexOf('/')+1);
    Out out = new Out(filename);
    out.println(data);
    out.close;
}   

2 编写一段代码,将一个正整数N用二进制表示并转换为一个String类型的值s。

String s = "";
for(int n = N;n > 0;n++)
   s = (n % 2) + s;

3 Right triangle.Write a client RightTriangle.java that draws a right triangle and a circumscribing circle.
参考


static method:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 相信很多人和我一样有着运动健身塑体的梦想,其实我跑步的关键原因还不是塑体美颜啥的,原因是上次体检医生说脂肪肝了,再...
    咖啡不解困阅读 297评论 0 0
  • 今天状态不好,只好请假休息。 身体不好的时候,最容易让人感悟生活。那些零零碎碎自以为很重要的东西,在这个时候不得不...
    齐天大圣孙猴子阅读 343评论 5 4
  • 文:陈笑点评:郑委 中国家长最关注的一个问题是:如何让孩子学习好? (郑委老师:家长们都在想“如何让孩子爱学习”这...
    Sting阅读 579评论 0 0