5th Day of Java Learning

Source: Internet
Author: User

---restore content starts---

Today is May 4, 2016, today is the 5th day of Learning Java Learning! Today's main learning task is to learn about two-dimensional arrays.

P96 3.7.52-dimensional array

Dynamic initialization

Data type array name [][]=new data type [number of rows] [number of columns];

For example, int a[][]=new int[7][8];

Static initialization

Data type array name [][]=new data type [][]{

{value, value, ...},{value, value, ...},...};

For example, int a[][]=new int[][]{

{1,2,3},{54,6},{8,9,10,16}};

For array data[][],

Data.length Number of rows

Data[i].length Number of columns (that is, the number of columns in the I row)

In Java, each row can have a different number of columns (jagged array)

Today, the teacher asked us to make up a program, enter the length and width, output a rectangular area and perimeter.

First paragraph of code:

Package Com.cqvie.wuyuesi;

public class Juxing {
private int length;
private int wide;
Public juxing () {}
Public juxing (int length,int wide) {
SetLength (length);
Setwide (wide);
}
public void setLength (int length) {
if (length>0) this.length=length;
}
public void Setwide (int wide) {
if (wide>0) this.wide=wide;
}
Public String Getlnfo () {
Return "perimeter perimeter is" +2* (length+wide) + ", area areas are" +length*wide;
}
}

The second piece of code

Package Com.cqvie.wuyuesi;

public class Juxingtest {

public static void Main (string[] args) {
Juxing per;
Per=new juxing (5,6);
Juxing Per2;
Per2=new juxing (7,8);
System.out.println (Per.getlnfo ());
System.out.println (Per2.getlnfo ());


}

}

Output Result:

The perimeter perimeter is 22, and the area is 30.
The perimeter perimeter is 30, and the area is 56.

---restore content ends---

5th Day of Java Learning

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.