一、创建数组
int[] a;数组的声明
new int[11];创建数组
创建数组的时候要指明数组的长度
二、数组的长度
数组的范围是0~4,第11行a[5]数组越界
三、初始化数组
四、选择排序法
package hello;
public class Helloworld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//选择法排序
//第一步: 把第一位和其他所有位进行比较
//如果发现其他位置的数据比第一位小,就进行交换
for (int i = 1; i < a.length; i++) {
if(a[i]<a[0]){
int temp = a[0];
a[0] = a[i];
a[i] = temp;
}
}
//把内容打印出来
//可以发现,最小的一个数,到了最前面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//第二步: 把第二位的和剩下的所有位进行比较
for (int i = 2; i < a.length; i++) {
if(a[i]<a[1]){
int temp = a[1];
a[1] = a[i];
a[i] = temp;
}
}
//把内容打印出来
//可以发现,倒数第二小的数,到了第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//可以发现一个规律
//移动的位置是从0 逐渐增加的
//所以可以在外面套一层循环
for (int j = 0; j < a.length-1; j++) {
for (int i = j+1; i < a.length; i++) {
if(a[i]<a[j]){
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
冒泡排序法
package hello;
public class Helloworld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//冒泡法排序
//第一步:从第一位开始,把相邻两位进行比较
//如果发现前面的比后面的大,就把大的数据交换在后面
for (int i = 0; i < a.length-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//把内容打印出来
//可以发现,最大的到了最后面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//第二步: 再来一次,只不过不用比较最后一位
for (int i = 0; i < a.length-2; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//把内容打印出来
//可以发现,倒数第二大的到了倒数第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//可以发现一个规律
//后边界在收缩
//所以可以在外面套一层循环
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length-j-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
五、复制数组
System.arraycopy(src, srcPos, dest, destPos, length)
src: 源数组
srcPos: 从源数组复制数据的起始位置
dest: 目标数组
destPos: 复制到目标数组的起始位置
length: 复制的长度
package hello;
public class Helloworld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
int b[] = new int[3];//分配了长度是3的空间,但是没有赋值
//通过数组赋值把,a数组的前3位赋值到b数组
//方法一: for循环
for (int i = 0; i < b.length; i++) {
b[i] = a[i];
}
//方法二: System.arraycopy(src, srcPos, dest, destPos, length)
//src: 源数组
//srcPos: 从源数组复制数据的启始位置
//dest: 目标数组
//destPos: 复制到目标数组的启始位置
//length: 复制的长度
System.arraycopy(a, 0, b, 0, 3);
//把内容打印出来
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
获取随机数(int) (Math.random() * 100)
Math.random() 会得到一个0-1之间的随机浮点数
六、定义一个5X5的二维数组。 然后使用随机数填充该二维数组。
找出这个二维数组里,最大的那个值
package hello;
import org.omg.Messaging.SyncScopeHelper;
public class Helloworld {
public static void main(String[] args) {
//int[][]b=new int[5][5];
int a[][]=new int[5][5];
int b[][]=new int[][]{{(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100)},{(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100)},{(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100)},{(int)(Math.random()*100),
(int)(Math.random()*100),
(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100)},{(int)(Math.random()*100),
(int)(Math.random()*100),
(int)(Math.random()*100),(int)(Math.random()*100),
(int)(Math.random()*100)}};
int max=0;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(b[i][j]+" ");
if(max<b[i][j])
max=b[i][j];
}
System.out.println();
}
System.out.println("max="+max);
}
}