导航:首页 > 编程语言 > java数组扩展

java数组扩展

发布时间:2024-10-04 01:15:07

java中数组有没有length()方法string没有lenght()方法

java中数组是没有length()方法的,只有length属性,数组array.length返回的是该数组的长度。

字符串String是有length()方法的,str.length()返回的是该字符串的长度。

(1)java数组扩展扩展阅读

java数组常用方法:

1、声明一个数组

String[] aArray = new String[5];

String[] bArray = {"a","b","c", "d", "e"};

String[] cArray = new String[]{"a","b","c","d","e"};

2、打印一个数组

String[] aArray = new String[5];

String[] bArray = {"a","b","c", "d", "e"};

String[] cArray = new String[]{"a","b","c","d","e"};

3、根据数组创建ArrayList

String[] stringArray = { "a", "b", "c", "d", "e" };

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));

System.out.println(arrayList);

4、判断数组内部是否包含某个值

String[] stringArray = { "a", "b", "c", "d", "e" };

boolean b = Arrays.asList(stringArray).contains("a");

System.out.println(b);

5、连接两个数组

int[] intArray = { 1, 2, 3, 4, 5 };

int[] intArray2 = { 6, 7, 8, 9, 10 };

int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

6、声明一个内联数组

method(new String[]{"a", "b", "c", "d", "e"})

String常用方法:

1、求字符串某一位置字符

charAt(int index)返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是

length()-1。

例如:

String str = new String("asdfzxc");

char ch = str.charAt(4);//ch = z

2、提取子串

用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

1)substring(int beginIndex)该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一

个新的字符串返回。

2)substring(int beginIndex, int endIndex)该方法从beginIndex位置起,从当前字符串中取出到

endIndex-1位置的字符作为一个新的字符串返回。

例如:

String str1 = new String("asdfzxc");

String str2 = str1.substring(2);//str2 = "dfzxc"

String str3 = str1.substring(2,5);//str3 = "dfz"

3、字符串比较

1)compareTo(String anotherString)该方法是对字符串内容按字典顺序进行大小比较,通过返回的

整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负

整数,相等返回0。

2)compareToIgnore(String anotherString)与compareTo方法相似,但忽略大小写。

3)equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回

true,否则返回false。

4)equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

例如:

String str1 = new String("abc");

String str2 = new String("ABC");

int a = str1.compareTo(str2);//a>0

int b = str1.compareToIgnoreCase(str2);//b=0

boolean c = str1.equals(str2);//c=false

boolean d = str1.equalsIgnoreCase(str2);//d=true

4、字符串连接

concat(String str)将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

例如:

String str = "aa".concat("bb").concat("cc");

相当于String str = "aa"+"bb"+"cc";

② java中数组最多可以放置多少个元素

java数组用int做引索,最大容量2G=2 147 483 639个元素空间。
同时受“可用内存空间”的大小限制。
java每个对象元素实际占用内存都大于12字节,即使你有16GB内存,也不够2G个对象。

阅读全文

与java数组扩展相关的资料

热点内容
子午线指标源码 浏览:238
浮点数乘幂运算法 浏览:318
pdf爬虫 浏览:557
php按钮点击事件 浏览:934
河南服务器机房售后服务云主机 浏览:184
android仿联系人 浏览:368
什么app软件可以学音标 浏览:669
郭天祥十天学会单片机优酷 浏览:330
什么app儿童免费 浏览:582
遗传算法的理解 浏览:800
php删除sql 浏览:841
红进蓝出指标源码 浏览:700
python数据转换列表类型 浏览:717
解压后的文件怎么解开 浏览:175
四川补贴认证下载什么app 浏览:858
android设计风格 浏览:426
视频不支持我的加密 浏览:342
布包pdf 浏览:267
程序员录制课程表 浏览:626
eclipsephp断点调试 浏览:895