Java 一维数组作为参数和返回值
2024-10-19 08:57:41
一维数组作为参数:
- 传数组的引用
- 创建数组直接传,本质也是传数组的引用
- 传null
public class Test { //数组作为参数时,可以传递3中形式
public void m1(int[] a) {
System.out.println("数组长度是:"+ a.length);
} public static void main(String[] args) {
Test t = new Test(); //创建一个数组,传递数组引用
int[] b = {1,2,3,4,5};
t.m1(b);
//直接创建数组传值
t.m1(new int[]{1,2,3});
//直接传递null,但是次数组不可用
t.m1(null);
}
}
一维数组作为返回值:
- 返回数组的引用
- 直接创建一个数组返回,本质上是返回数组的引用
- 返回null
public class Test { //返回数组的引用
public String[] m1() {
String[] s = {"abc","de"};
return s;
} //返回直接创建的数组
public String[] m2() {
return new String[]{"a", "b","c"};
} //返回null
public String[] m3() {
return null;
} public static void main(String[] args) {
Test t = new Test(); String[] s1 = t.m1();
System.out.println("接收到的数组长度:" + s1.length);
String[] s2 = t.m2();
System.out.println("接收到的数组长度:" + s2.length);
String[] s3 = t.m3();
System.out.println("接收到的数组长度:" + s3.length);
}
}
最新文章
- Java关于Properties用法的总结(一)
- centos7 hostname
- dispay属性的block,inline,inline-block
- 为什么Web 设计会‘死’?
- Qt之QSizePolicy
- Oracle 中的 TO_DATE 和 TO_CHAR 函数
- online ddl 跟踪
- C# to Maxscript
- C# this关键字详解
- 【C语言】printf函数详解
- Missing Ranges 解答
- 一个action读取另一个action里的session
- 【R】正态检验与R语言
- ASP.NET Core 2.1对GDPR的支持
- es6那些事儿
- Jmeter也能IP欺骗!
- windows(xshell)免密码登录
- sitecore系统教程之Item快速了解
- Luogu P1558 色板游戏
- python变量名感悟
热门文章
- ping 127.0.0.1和ping本地ip分别测试什么?
- (16)Python练习题
- swift的调用约定
- 在linux系统下安装配置apacheserver
- 统计单词数 OpenJ_Bailian - 4030(字符串处理)
- css3属性中background-clip与background-origin的用法释疑
- go标准库的学习-crypto/sha1
- 让sublime text3支持Vue语法高亮显示
- 【Codeforces 1132F】Clear the String
- 利用git向github上远程提交一个自己的开源项目