对象数组的初始化
2016-05-03 16:29
267 查看
public static void main(String[] args) { String[] str = new String[10];//1 初始化数组 for (int i = 0; i < 10; i++){ System.out.println(str[i]);//打印出来的是null } for (int i = 0; i < 10; i++){ str[i] = new String("Hello");//2 为对象初始化 } for (int i = 0; i < 10; i++){ System.out.println(str[i]);//打印出来的是Hello } }
1初始化数组:
它是创建了是个String类型的变量:str[0], str[1],….str[9]
但是没有为它们创建对象,所以为它们的初始值都为null
2为对象初始化:
str[i] = new String(“Hello”);创建了string对象,并让
变量str[i]引用这个对象
对象数组初始化的第二种方法
String[] s = new String[]{"He", "me", "we", "sa", "she"};
相当于:
String[] s= new String[5]; s[0] = "He"; s[1] = "me"; s[2] = "we"; s[3] = "sa"; s[4] = "she";
相关文章推荐
- java c++ 程序员 技术面试常用问题
- nyoj_36 最长公共子序列
- hadoop日志存放路径(日志讲解好文章)&hadoop历史服务器
- 从此不求人:自主研发一套PHP前端开发框架(29)
- ASP.NET WebAPI 自动生成帮助文档
- LoadRunner 实现监控Tomcat
- .net的简易多线程处理
- Calabash-android环境搭建
- 猫都能学会的Unity3D Shader入门指南(二)
- glib交叉编译
- 自定义ViewPagerIndicater
- bootstrap 模态框 遮住了 ueditor 编辑器的表情框
- iOS instruments
- Swift 单例
- Session
- python cx_oracle 取回数据后 LOB variable no longer valid after subsequent fetch
- 27. Remove Element
- Visual Studio 2013 的 Xamarin 安装教程
- 复合数据类型(简答)
- 防御性编程