安卓培訓(Java篇)第三天

來源:互聯網
上載者:User

標籤:java   空間   元素   

  1. 棧記憶體:存放局部變數;運行效率較高,生命週期短;

    堆記憶體:存放所有new出來的對象;運行效率較低,生命週期長;

    代碼區:對象調用方法時才載入;一般存放靜態和非靜態方法;

    常量池:堆棧共用,可以用來傳值。

    650) this.width=650;" src="http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/1037D3787A2647808DEA80A5C20590CE/3085FAF6867444E6B9EF071EFE7019C1" style="height:auto;width:800px;" alt="3085FAF6867444E6B9EF071EFE7019C1" />

  2. 數組:

    概念:有一定長度的同種類型元素的集合;

    分類:一維,二維;

    存放位置:堆記憶體;

    備忘:所有參考型別在堆記憶體裡面都有自己的空間。

  3. (1)一維數組文法:


    靜態:int[] a={1,2,3,4}; int a[]=new int[]{1,2,3,4};

    動態:int[]b;  int b[]=new int[4];

    排序:冒泡排序法、比較法;(兩兩做比較,利用加法交換律來交換值)

    注意事項:數組下標從0開始,小心下標的越界

    數組長度:數組名.length;

    

   (2)二維數組:

    靜態:int[][] a={{1},{2,3},{4,5,6}}; int[][] a=new int[][]{{1},{2,3},{4,5,6}};

    動態:int[][] a; int[][] a=new int[4][]; int[][] a=new int[3][4];

    注意事項:二維數組的長度等價於行數,相當於上述例子int[][] a=new int[4][];中“4”就是二維數組a的長度;

    理解二維數組包含一維數組;所有數組的遍曆都必須有迴圈。


課後練習:

  1. 用Scanner任意輸入兩個數,求它們的最大公約數和最小公倍數,請用兩個方法分別來表示最大公約數和最小公倍數。


代碼:

import java.util.Scanner;

public class A {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("輸入第一個數:");

int a = s.nextInt();

System.out.println("輸入第二個數:");

int b = s.nextInt();

System.out.println("最大公約數:" + gongYueShu(a, b));

System.out.println("最小公倍數:" + gongBeiShu(a, b));

}


public static int gongYueShu(int a, int b) {

int in, min = 0;

if (a > b) {

in = b;

} else {

in = a;

}

for (int i = 1; i <= in; i++) {

if (a % i == 0 && b % i == 0) {

min = i;

}

}

return min;

}


public static int gongBeiShu(int a, int b) {

int gb = a * b / gongYueShu(a, b);

return gb;

}

}


2.

看如下數列,求值;

1,2,7,13,49,24,343……前20項之和;


代碼:

public class A {

public static void main(String[] args) {

int a = 1, b = 2;

int sum = 0, sum1 = 0, sum2 = 0;

for (int i = 1; i <= 20; i++) {

if (i % 2 == 0) {

sum1 = sum + b;

b = b + 11;

} else {

sum2 = sum + a;

a = a * 7;

}

}

sum = sum1 + sum2;

System.out.println("前20項和:" + sum);

}

}


3.冒泡法排序:

將給定數字序列從小到大排序:

public class A {

public static void main(String[] args) {

int a[] = { 3, 2, 5, 1, 7 };

int b;

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

for (int j = 0; j < a.length - 1; j++) {

if (a[j] > a[j + 1]) {

b = a[j + 1];

a[j + 1] = a[j];

a[j] = b;

}

}

}

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

System.out.print(a[i] + " ");

}

}

}


4.任意輸入3個數,並輸出最大的那個數


public class A {

public static void main(String[] args) {

System.out.println("請輸入3個整數:");

Scanner s = new Scanner(System.in);

int a = s.nextInt();

int b = s.nextInt();

int c = s.nextInt();

if (a > b) {

if (a > c) {

System.out.println("最大的數為" + a);

} else {

System.out.println("最大數為" + c);

}

} else {

if (b > c) {

System.out.println("最大數為" + b);

} else {

System.out.println("最大數為" + c);

}

}

}

}













本文出自 “坤” 部落格,請務必保留此出處http://linyingkun.blog.51cto.com/2393912/1574521

安卓培訓(Java篇)第三天

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.