学习笔记(05):Java小白修炼手册-不会不行!开发中99.9%会用到的数组

立即学习:https://edu.csdn.net/course/play/27274/361061?utm_source=blogtoedu

Java数组

一、   声明数组

1,如int [ ] arr = {1,2,3,};

或者int  [ ] arr2 =new int [5];

访问数组用数组名【下标】;  

注意字符串和数组的长度获取的方式不同

字符串是字符串名.length()//这里是length()方法;而数组用数组名.length

二、数组的使用

数组可以作为参数传给一个方法,还可以作返回值

1、数组的遍历,

int 【】arr={1,2,3,4,5};

for each循环 for(int i:数组名)

sysout (i);

2、数组作参数

 string [ ]names={"name","kite","tom"};

//在主方法中声明,然后在另一个方法中调用

static void  test array(string[ ] a)

{for(string i:a){

sysout(i);}

}

3.数组作返回值

Arrays工具类

Arrays.sort(数组名);排序

int index=Arrays.binarySearch(数组名,查找数)(返回查找数字的位置)

猜你喜欢

转载自blog.csdn.net/weixin_46273997/article/details/104227802