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

Python013

java中数组有没有length()方法?string没有lenght()方法?,第1张

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

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

扩展资料

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";

length是数组才有的属性,返回数组元素的个数,如果不是数组就不能用length,只要记数组是种java数据结构,它提供一个可以知道其元素多少的属性length,这就是规则,没什么好解释的,有些东西需要理解记,有些就是记