The third day of Android training (Java article)

Source: Internet
Author: User
Tags greatest common divisor

    1. Stack memory: Storage of local variables, high efficiency, short life cycle;

      Heap Memory: All new objects are stored, the running efficiency is low, the life cycle is long;

      Code area: The object is loaded only when the method is called, and static and non-static methods are generally stored;

      Chang: The stack is shared and can be used to pass values.

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

    2. Array:

      Concept: A set of elements of the same type with a certain length;

      Classification: one-dimensional, two-dimensional;

      Storage location: heap memory;

      Note: All reference types have their own space in the heap memory.

    3. (1) One-dimensional array syntax:


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

Dynamic: int[]b; int b[]=new int[4];

Sort: Bubble Sort method, Comparison method; (22 Compare, use the addition Exchange law to exchange values)

Note: array subscript starting from 0, carefully subscript out of bounds

Array Length: array name. length;


(2) Two-dimensional arrays:

Static: int[][] a={{1},{2,3},{4,5,6}}; Int[][] A=new int[][]{{1},{2,3},{4,5,6}};

Dynamic: int[][] A; Int[][] A=new int[4][]; Int[][] A=new int[3][4];

Note: The length of the two-dimensional array is equivalent to the number of rows, equivalent to the above example int[][] a=new int[4][]; "4" is the length of the two-dimensional array A;

Understand that two-dimensional arrays contain one-dimensional arrays, and that all arrays must have loops in their traversal.


After-school exercises:

    1. Use scanner to enter two numbers arbitrarily, greatest common divisor and least common multiple, please use two methods to represent greatest common divisor and least common multiple respectively.


Code:

Import Java.util.Scanner;

public class A {

public static void Main (string[] args) {

Scanner s = new Scanner (system.in);

System.out.println ("Enter first number:");

int a = S.nextint ();

System.out.println ("Enter a second number:");

int b = S.nextint ();

System.out.println ("Greatest common divisor:" + Gongyueshu (A, b));

SYSTEM.OUT.PRINTLN ("Least common multiple:" + 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.

See the following series, evaluation;

1,2,7,13,49,24,343 ... The sum of the first 20 items;


Code:

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 <=; i++) {

if (i% 2 = = 0) {

SUM1 = sum + b;

B = B + 11;

} else {

sum2 = sum + A;

A = a * 7;

}

}

sum = sum1 + sum2;

System.out.println ("Top 20 items and:" + sum);

}

}


3. Sort by bubbling:

To sort a given number sequence from small to large:

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. Enter any 3 numbers and output the maximum number


public class A {

public static void Main (string[] args) {

System.out.println ("Please enter 3 integers:");

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 ("The largest number is" + a);

} else {

SYSTEM.OUT.PRINTLN ("Maximum number is" + C);

}

} else {

if (b > C) {

SYSTEM.OUT.PRINTLN ("Maximum number is" + B);

} else {

SYSTEM.OUT.PRINTLN ("Maximum number is" + C);

}

}

}

}













This article is from the "Kun" blog, please be sure to keep this source http://linyingkun.blog.51cto.com/2393912/1574521

The third day of Android training (Java article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.