每天学JAVA——数组

一、数组排序

冒泡原理(由小到大)

Public class TestDemo{

Public static void mian(String [] args){

    int data [] = new int []{1,3,2,6,10,0,5,8};

    sort(data);

    print (data);

}

Public void sort(int temp[]){

    for ( x = 0 ,x < temp.length, x++){

    for (y = 0,y < temp.length - 1,y++){

    if (temp[y]>temp[y+1]){

        int t = temp[y] ;

        temp[y] = temp[y+1] ;

        temp[y+1] = t ;

    }

}

}

}

Public void print(int temp[]){

for(int x = 0,x < temp.length,x++){

    System.out.print(temp[x] + );

}

System.out.println();

}

}

也可以利用“java.util.Arryays.sort(数组)”可以完成排序。


冒泡原理图:


二、要求定义一个方法,这个方法可以统计出数组的最大值、最小值、总和和平均值(忽略小数)。

public class  TestDemo{

public static void  mian(String args []){

int data[] = new int [] {6,10,3,8,15,12};

int st[] = stat(data);

System.out.println("最大值:" + st[0]);

System.out.println("最小值" + st[1]);

System.out.println("总和" + st[2]);

System.out.println(“平均值" + st[3]);

}

public static int[] stat(int temp[]){

int result[4] = new int [4] ;

result[0] = temp[0];

result[1] = temp[0];

for(int x = 0, x<temp.length, x++){

if( result[0] < temp[x] ){//更改最大值

result[0] = temp[x];

}

if( result[1]] > temp[x] ){//更改最小值

result[1] = temp[x];

}

result[2] += temp[x];//求总和

}

result[3] = result[2] / temp.length;//求平均值

 return result ; 

}

}

三、与数组有关的操作方法

(1)数组排序:java.util.Arrays.sort(数组);

(2)数组拷贝:System.ayyaycopy(源数组名称,源数组开始点,目标数组名称,目标数组开始点,拷贝长度);

四、对象数组

定义格式和之前一样,只要把数据类型换成类即可。

五、习题

(1)将一个给定的一维数组转置输出,例如,源数组内容:1、2、3、4、5、6;转置之后的数组: 6、5、4、3、2、1。

实现方法一:开辟一个新数组,将原数组的内容倒置输出到新数组之中,而后丢次原数组。但是这种转置方式在操作过程中会产生垃圾空间,所以不推荐使用。

实现方法二:在数组上执行首尾交换,由于此时在一个数组上完成,所以不会产生垃圾。

public class TestDemo{

public static void mian(String args[]){

int data[] = new int []{1,2,3,4,5,6};

reverse(data);

print(data);

}

public static  void reverse(int temp[]){

int head = 0;

int tail = temp.length;

for(int x = 0,x < temp.length/2,x++){

int t = temp[tail];

temp[tail] = temp[head];

temp[head] = t;

head++;

tail--;

}

}

public static void print(int temp[]){

for(int x = 0, x < temp.length,x++){

system.out.print(temp[x] + "、");

}

systemo.out.println();

}

}

(2)常见面试题:

请写出二维数组的行列转置

public class TestDemo{

public static void main(String [] args){

int data [] [] = new int [] [] {{1,2,3},{4,5,6},{7,8,9}} ;

reverse(data);

print(data);

}

public static void reverse(int temp[] []){

for( int x = 0, x<temp.length,x++){

for(int y = x,y < temp[x].length,y++){

int t = temp[x][y];

temp[y][x] = temp[x][y];

temp[x][y] = t ;

}

}

}

public static void print(int temp[][]){

for(int x = 0,x<temp.length,x++){

for(int y = 0,y < temp[x].length,y++){

system.out.print(temp[x][y]);

}

system.out.println();

}

}

}


(3)现在有如下的一个数组:

int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

    要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为int newArr[] = {1,3,4,5,6,6,5,4,7,6,7,5};

思路:

   首先需要知道新数组的长度,所以要统计oldArr数组中除了0之外的元素个数;

   开辟了新数组,将不为0的数组保存到新数组之中,拷贝原理如下

代码实现:数组去0

public class TestDemo{

public static void mian(String args []){

int oldArr[] = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

int newArr[] = new int [count(oldArr)];

copy(oldArr,newArr);

print(newArr);

}

public static int count(int temp[]){

int sum = 0;

for(int x = 0, x < temp.lenrth,x++){

if(temp[x] != 0){

sum ++;

}

}

return sum;

}

public static void copy(int  old[],int new[]]){

int y =0;

for(int x =0,x <old.length,x++){

if(old[x] != 0){

new[y] = old[x];

y++;

}

}

public static void print(int temp[]){

for(int x = 0,x < temp.length,x++){

system.out.print(temp[x]+"、");

}

system.out,println();

}

}































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值