- 数组工具类 Arrays.toString() 遍历输出一个数组
@Test
public void test01(){
String[] a0 = {"你","好","北","京"};
System.out.println(Arrays.toString(a0));
}
- Arrays.copyOf(数组名,长度 ); 拷贝一个数组 其长度可以自己定义
@Test
public void test01(){
String[] a0 = {"你","好","北","京"};
System.out.println(Arrays.toString(a0));
String[] a1 = Arrays.copyOf(a0, 6);
System.out.println(Arrays.toString(a1));
}
现拷贝a0 定义下标长度为6 定义为新数组a1 空的部分会显示null
若定义长度为 2 则只显示 “你” “好”
- 方法简介
/**
* 方法 就是一段可以重复被执行的代码的封装
* 方法五要素: 修饰符 返回值类型 方法名 参数列表 方法体
* 如下所示
* public 访问修饰符
* static 静态修饰符
* void 返回值占位符
* main 方法名
* String[] 是参数的数据类型 args 是参数名称
* {} 是方法体
* @author dwc
* @version 1.0.0
* 2022 2022/4/20 下午9:42
*/
public class Demo03 {
public static void main(String[] args) {
System.out.println("坐看风生水起"); //最简单的输出
}
}
- 无参数和返回值的方法 没有返回值时用void占位符 有返回值时要用对应的数据类型
public static void demo01(){
System.out.println("人间好时节");
}
- 没有参数 有返回值 此示例中 return后面是String类型 返回值类型要为String
public static String demo02(){
return "千里之行始于足下";
}
- 有参数 没有返回值的
/**
* 有参数 没有返回值
* @param food 食物
*/
public static void demo03(String food){
System.out.println("吃"+food);
}
- 有参数 有返回值
/**
* 有参数 有返回值
* @param name 名字
*/
public static String demo04(String name){
return "我的名字是" + "\t" + name;
}
- 两个参数 无返回值
/**
* 两个参数 无返回值
* @param count 数量
* @param food 食物名
*/
public static void demo05(int count, String food){
System.out.println("吃" + count + "个" + food);
}
- 多个参数 有返回值的
/**
* 多个参数 有返回值
* @param a 加数
* @param b 被加数
*/
public static int demo06(int a,int b){
return a + b;
}
- 数组的遍历
public static void demo07(int[] a){
System.out.print("[");
for (int i = 0; i < a.length; i++) {
if (i < a.length - 1){
System.out.print(a[i] + ",");
}else{
System.out.print(a[i]);
}
}System.out.print("]");
}
所有demo的结果