java 返回2个数组_java:数组作返回值/两个数组判断相同/数组的复制和截取.

package com.itheima.kuihuabaodian;

import java.util.Scanner;

public class Case08 {

public static void main(String[] args) {

// 输入两个5个元素的数组

Scanner num = new Scanner(System.in);

int[] arr1;

int[] arr2;

arr1=defArray(5);

arr2=defArray(5);

equals(arr1,arr2);

System.out.println("请输入一个值:");

fill1(arr1,num.nextInt());

showArray(arr1,5);

System.out.println("请输入一个值:");

fill2(arr2,3,5,num.nextInt());

showArray(arr2,5);

int[] arr3= copyOf(arr2,3);

showArray(arr3,3);

int[] arr4=copyOfRange(arr2,2,5);

showArray(arr4,5);

}

// 定义:比较数组是否相等的方法

public static void equals(int[] arr1,int[] arr2){

if(arr1.length==arr2.length){

int count=0;

for(int i=0;i

if(arr1[i]==arr2[i]){

count++;

}

}

System.out.println(count);

System.out.println(arr1.length);

if(count==arr1.length){

System.out.println("两数组相等");

}else{

System.out.println("两数组不相等");

}

}

}

// 定义:将一个数组元素中的值都改为人为输入的值

public static void fill1(int[] arr1,int n){

for (int i=0;i

arr1[i]=n;

}

}

/* 定义:将数组arr1中的元素从索引fromindex开始到

toindex(不包含toindex)对应值改为value. */

public static void fill2(int[] arr1,int fromindex,int toindex,int value){

Scanner num = new Scanner(System.in);

while (true){

if(toindex>fromindex){

break;

}else {

System.out.printf("toindex小于fromindex,请重新输入:");

toindex=num.nextInt();

}

}

for (int i=fromindex;i< toindex;i++){

arr1[i]=value;

}

}

/*定义一个方法copyOf(int[] arr, int newLength),功能

:将数组arr中的newLength个元素拷贝到新数组中,

并将新数组返回,从索引为0开始

*/

public static int[] copyOf(int[] arr1,int n){

int[] arr2=new int[n];

for(int i=0;i

arr2[i]=arr1[i];

}

return arr2;

}

/*定义一个方法copyOfRange(int[] arr,int from, int to),

功能:将数组arr中从索引from(包含from)开始

到索引to结束(不包含to)的元素复制到新数组中,并将新数组返回

*/

public static int[] copyOfRange(int[] arr,int from,int to){

int[] arr1=new int[arr.length];

for(int i=from;i

arr1[i]=arr[i];

}

return arr1;

}

/* 这段代码是将一元数组出的截取片段生成新的数组

使用时需要将函数的参数设置成子数组的大小,否则会产生溢出错误.showArray(arr4,3)

public static int[] copyOfRange(int[] arr,int from,int to){

int[] arr1=new int[to-from];

int i,j;

for(i=from,j=0;i

arr1[j]=arr[i];

}

return arr1;

}

*/

// 输出一元数组的每个元素

public static void showArray(int[] arr,int n){

for(int i=0;i

System.out.print(arr[i]+" ");

}

System.out.println();

}

//定义int型一元数组

public static int[] defArray(int n){

Scanner num = new Scanner(System.in);

int[] arr=new int[n];

for (int i=0;i

System.out.println("请输入数组中第"+(i+1)+"个数组元素的值:");

arr[i]=num.nextInt();

}

return arr;

}

}

代码输出:

"D:\IDEA\IntelliJ IDEA 2019.3.3\jbr\bin\java.exe" "-javaagent:D:\IDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=63878:D:\IDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\IDEA\IDEAcode\baseclass\out\production\day05code com.itheima.kuihuabaodian.Case08

请输入数组中第1个数组元素的值:

1

请输入数组中第2个数组元素的值:

1

请输入数组中第3个数组元素的值:

1

请输入数组中第4个数组元素的值:

1

请输入数组中第5个数组元素的值:

1

请输入数组中第1个数组元素的值:

2

请输入数组中第2个数组元素的值:

2

请输入数组中第3个数组元素的值:

2

请输入数组中第4个数组元素的值:

2

请输入数组中第5个数组元素的值:

2

0

5

两数组不相等

请输入一个值:

3

3 3 3 3 3

请输入一个值:

4

2 2 2 4 4

2 2 2

0 0 2 4 4

Process finished with exit code 0

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值