package com.jsu.www;
import java.lang.reflect.Array;
public class New04 {
public static void main(String[] args) {
int arr[]={11,22,33,44,55};//明确要打印的对象
printfArr(arr);//定义一个方法来打印
}
public static void printfArr(int arr[]) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i== arr.length-1)
System.out.print(arr[i]);
else
System.out.print(arr[i]+",");
}
System.out.print("]");
}
}
package com.jsu.www;
import java.util.Scanner;
public class New05 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int arr[]={1,2,3,4,5};
int num=scanner.nextInt();//将要判断的数输入
System.out.println(contain(arr,num));//注意不要写arr[]
}
public static boolean contain(int arr[],int num ) {
for (int i = 0; i < arr.length; i++) {//遍历数组每一个元素
if(num==arr[i])
return true;
}
return false;
}
}
package com.jsu.www;
public class New06 {
public static void main(String[] args) {
int arr[]={1,2,3,4,5,6};
int copyArr[]=copyOfRange(arr,0,3);
for (int i = 0; i < copyArr.length; i++) {
System.out.print(copyArr[i]+" ");
}
}
public static int[] copyOfRange(int arr[],int from,int to ) {
int newArr[]=new int[to-from];//动态分配
int index=0;
for (int i = from; i < to; i++) {
newArr[index]=arr[i];
index++;
}
return newArr;
}
}
标签:arr,int,练习,System,三个,static,关于,public,out
From: https://blog.csdn.net/2301_80899232/article/details/136789135