Java-数组和String类

数组

数组的定义

三种定义的方式:
1·数据类型[] 数组名 = new 数据类型 [长度]
2·数据类型[] 数组名 = new 数据类型[]{元素列表}
3·数据类型[] 数组名 = {元素列表}

数组的访问

可以通过索引来访问数组中的元素,第一个数组的索引值为0,所以数组的最大索引值为.length-1


String类

String代表字符串,java中所有的字符串文字,都被看成是该类的实例。

创建字符串的方法

1·无参构造:String str = new String();
2·通过字符数组构造:
char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
3·通过字节数组构造:
byte bytes[] = { 97, 98, 99 }; 
String str3 = new String(bytes);

常用方法

public boolean equals (Object anObject) :将此字符串与指定对象进行比较

public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。

public int length () :返回此字符串的长度。

public String concat (String str) :将指定的字符串连接到该字符串的末尾。

public char charAt (int index) :返回指定索引处的 char值。

public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。

public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。

public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。

public char[] toCharArray () :将此字符串转换为新的字符数组。

public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。

public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。

Arrays类

概述:java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和索等。其所有方法均为静态方法

操作数组的方法

public static String toString(int[] a) :返回指定数组内容的字符串表示形式

public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值