package com.shxt.demo01;
public class ArrayDemo02 {
public static void main(String[] args) {
int[] score = null; //声明数组,但是为开辟内存空间
score = new int[5]; //为数组开辟"堆内存"空间
for (int i = 0; i < 5; i++) {
score[i] = i*2+1;
}
for (int i = 0; i < 5; i++) {
System.out.println("score["+i+"]="+score[i]);
}
}
}
数组长度的取得
数组名称.length --> 返回一个int型的数据
package com.shxt.demo01;
public class ArrayDemo03 {
public static void main(String[] args) {
int[] score = new int[5];
System.out.println("数组长度为:"+score.length);
}
}
(3) 数组的初始化方式
动态初始化
之前练习的就是使用的动态初始化方式
package com.shxt.demo01;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] score = null; //声明数组,但是为开辟内存空间
score = new int[3]; //为数组开辟"堆内存"空间
score[0] = 100;
score[1] = 200;
score[2] = 300;
}
}
静态初始化
数据类型[] 数组名={初始值0,初始值1,...,初始值N}
或者
数据类型[] 数组名 = new 数据类型[]{初始值0,初始值1,...,初始值N}
package com.shxt.demo01;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] score = {10,20,30,40,50};
for (int i = 0; i < score.length; i++) {
System.out.println("score["+i+"]="+score[i]);
}
}
}
(4) 课堂练习
已知一个字符数组 char[] letterArray = new char[26] ,请动态初始化数据为A~Z
package com.shxt.demo01;
public class ArrayDemo05 {
public static void main(String[] args) {
char[] letterArray = new char[26];
for (int i = 0; i < letterArray.length; i++) {
letterArray[i] = (char)('A'+i);
}
for (int i = 0; i < letterArray.length; i++) {
System.out.print(letterArray[i]+"\t");
}
}
}
请求数组中最大和最小值
package com.shxt.demo01;
public class ArrayDemo06 {
public static void main(String[] args) {
int array[]={74,12,48,888,30,5,17,62,777,666};
int max = array[0];
int min = array[0];
for (int i = 0; i < array.length; i++) {
if(array[i]>max){
max=array[i];
}
if(array[i]<min){
min=array[i];
}
}
System.out.println("最高成绩:"+max);
System.out.println("最低成绩:"+min);
}
}
对整数数组按照由小到大的顺序进行排序
public class ArrayDemo07 {
public static void main(String[] args) {
int arr[] = {13,34,57,78,99,66,43,45,87,100};
for (int i = 0 ; i < arr.length; i++) {
for (int j = 0; j < arr.length-1; j++) {
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.print("第"+i+"次 排序 的结果:\t");
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+"\t");
}
System.out.println();
}
System.out.print("最终的排序结果:\t");
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+"\t");
}
}
}
数据类型[][] 数组名 = new 数据类型[[]{{值1,值2},{值1,值2,值3...},{值1...}};
遍历二维数组
package com.shxt.demo01;
public class ArrayDemo11 {
public static void main(String[] args) {
int[][] data = new int[][]{{1,2,3},{10},{100,200}};
for (int i = 0; i < data.length; i++) { //循环行数
for (int j = 0; j < data[i].length; j++) { //循环列数
System.out.print(data[i][j]+"\t");
}
System.out.println();
}
}
}
In order to understand the framework in the context of a real-world application, we need to build something that will more closely resemble the types of applications web developers actually have to bu......一起来看看 《Agile Web Application Development with Yii 1.1 and PHP5》 这本书的介绍吧!