【java】各种方法的使用(不定期更新)


疑难杂症篇

1、输入一个字符的方法 问题在于Scanner中没有一个类似于nextInt()之类的函数。

代码实现

char num = scanner.next().charAt(0);

2、将字符串转化为数字 没啥问题就是记不下来
代码实现:

int a1 =Integer.parseInt(i1);

3、字符串转化为字符数组的方法

char[] strcs = text.toCharArray();

4、字符数组转化为字符串的方法(String(char value[], int offset, int count) 函数的使用)
value:需要变成字符串的字符数组
offset :初始偏移量
count:长度

char[] a={'a','d','f','r','d','e'};
         String s = new String(a,2,4);
         System.out.println(s);
   ///输出:frde

5、给所有的属性信息写gitter setter方法、重写toString方法(打印所有的属性信息)
alt + shift+ s

专题篇

String方法
1、startsWith(String value):
判断字符串是否以value字符串开头,如果是返回true,否则返回false
代码实现:

String a = "abc";
	
System.out.println(a.startsWith("a"));///返回值 true
	
System.out.println(a.startsWith("b"));///返回值 false

2、endsWith(String value)
代码实现:

String a = "abc";
	
System.out.println(a.endsWith("c"));
	
System.out.println(a.endsWith("b"));

3、equalsIgnoreCase(String value)
判断是否相同,忽略大小写

代码实现:

String a = "abc";
	
String b = "ABC";
	
System.out.println(a.equalsIgnoreCase(b));

4、replaceAll(String old,String new)
将当前字符串用new替换old
代码实现:

String a = "abcccc";
String a1 =a.replaceAll("c","a");
///输出 abaaaa
System.out.println(a1);

5、trim() 去除两边的空格
代码实现:

String a = " abcccc ";
String a1 =a.trim();
System.out.println(a);

6、split(String sign)的使用(附带加强for循环)
以sign为分隔将字符串分割成为字符数组
代码实现:

	String a = "ab:cc:cc ";
	String[] a1 =a.split(":");
	for(String num:a1) {
	/*输出
	ab
	cc
	cc
	*/
	System.out.println(num);
	}

7、indexOf(String s)的使用:
输出字符串s出现的第一个位置(从0开始计数),没有的话输出-1
代码实现:

String a = "abcds";
	
int a1 = a.indexOf(a);

//输出0	
System.out.println(a1);

8、lastIndexOf(String s)的使用:
最后一个位置 其余同上
代码实现:

String a = "abcds";
	
int a1 = a.lastIndexOf("ab");
	
System.out.println(a1);

9、indexOf(String s, int start)的使用:
从star开始第一次出现的位置

10、lastIndexOf(String s, int end)的使用:
到end结束

11、substring(int start)的使用:
从start开始截取到结尾
代码实现:

String a = "abcds";
	
String a1 = a.substring(2);
	
System.out.println(a1);

12、substring(int start, int end)

Character方法
Character的方法
(1)isDigit(char ch) 如果ch是数字返回true,否则返回false
(2)isLetter(char ch)如果ch是字母返回true 否则返回false
(3) isLetterOrDigit(char ch)
(4)isLowerCase(char ch) isUpperCase(char ch)是不是大小写
(5)toLowerCase(char ch) toUpperCase(char ch)返回大小写形式
(6)isSpaceChar(char ch)是否为空格

Collection方法
1) boolean add(E e)
确保此 collection 包含指定的元素(可选操作)。

(2) boolean addAll(Collection<? extends E> c)
将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。

(3) void clear()
移除此 collection 中的所有元素(可选操作)。

(4)boolean contains(Object o)
如果此 collection 包含指定的元素,则返回 true。

(5)boolean containsAll(Collection<?> c)
如果此 collection 包含指定 collection 中的所有元素,则返回 true。

(6)boolean equals(Object o)
比较此 collection 与指定对象是否相等。

(7)int hashCode()
返回此 collection 的哈希码值

(8) boolean isEmpty()
如果此 collection 不包含元素,则返回 true。

(9) Iterator iterator()
返回在此 collection 的元素上进行迭代的迭代器。

(10) boolean remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
如果存在相同的,则只删除第一个

(11) boolean removeAll(Collection<?> c)
移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。

(12) boolean retainAll(Collection<?> c)
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。
只要调用的方法改变就返回true

(13)int size()
返回此 collection 中的元素数。

(14)Object[] toArray()
返回包含此 collection 中所有元素的数组。

T[] toArray(T[] a)
返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

List方法
(1) boolean add(E e)
向列表的尾部添加指定的元素(可选操作)。

(2)void add(int index, E element)
在列表的指定位置插入指定元素(可选操作)。
如果插入到数组内部不用担心会越界,此时数组会自动增长,如果插入到数组外面且超过数组的长度,就会越界

(3) boolean addAll(Collection<? extends E> c)
添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序(可选操作)。

(4) boolean addAll(int index, Collection<? extends E> c)
将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。

(5) void clear()
从列表中移除所有元素(可选操作)。

(6)boolean contains(Object o)
如果列表包含指定的元素,则返回 true。

(7)boolean containsAll(Collection<?> c)
如果列表包含指定 collection 的所有元素,则返回 true。

(8)boolean equals(Object o)
比较指定的对象与列表是否相等。

(9)E get(int index)
返回列表中指定位置的元素。
当超过元素个数时会越界

(7) int hashCode()
返回列表的哈希码值。

(8) int indexOf(Object o)
返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。

(9) boolean isEmpty()
如果列表不包含元素,则返回 true。

(10) Iterator iterator()
返回按适当顺序在列表的元素上进行迭代的迭代器。

(11) int lastIndexOf(Object o)
返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
类比String类

(12)ListIterator listIterator()
返回此列表元素的列表迭代器(按适当顺序)。
注意区分,与Collection不同

(13) ListIterator listIterator(int index)
返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。

(14) E remove(int index)
移除列表中指定位置的元素(可选操作)。

(16) boolean remove(Object o)
从此列表中移除第一次出现的指定元素(如果存在)(可选操作)。

(17) boolean removeAll(Collection<?> c)
从列表中移除指定 collection 中包含的其所有元素(可选操作)。

(18) boolean retainAll(Collection<?> c)
仅在列表中保留指定 collection 中所包含的元素(可选操作)。

(19) E set(int index, E element)
用指定元素替换列表中指定位置的元素(可选操作)。

(20) int size()
返回列表中的元素数。

(21)List subList(int fromIndex, int toIndex)
返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。

(22)Object[] toArray()
返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。

(23) T[] toArray(T[] a)
返回按适当顺序(从第一个元素到最后一个元素)包含列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值