java笔记-集合框架-基本数据类型对象

基本数据类型对象封装类

为了方便 操作基本数据类型,将其封装成了对象,在对象中定义了属性和行为,丰富了数据的操作。

由于描述基本数据类型对象的类就称为基本数据类型对象封装类

byte                 Byte

short                hort

int                     Integer

long                 Long

float                 Float

double             Double

char                 Character

boolean           Boolean

包装类型主要用于基本数据类型和字符串之间的转换:

基本数据类型--->字符串:

1.基本数据类型+“”;

2.valueOf(基本数据类型)

字符串---->基本数据类型:

1. 使用包装类中的静态方法:XXX parseXXX("XXX类型的字符串");Character没有parse方法。

2. 如果int型的数值被封装成了对象,可以使用IntValue()转化为int

十进制--->其它进制

static String toBinaryString(int i)

以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。 

static String toHexString(int i) 

以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。 

static String toOctalString(int i) 

以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。 

static String toString(int i, int radix) 

返回用第二个参数指定基数表示的第一个参数的字符串表示形式。 

其它进制--->十进制

static int parseInt(String s, int radix) 

使用第二个参数指定的基数,将字符串参数解析为有符号的整数。 

关于新建的Integer对象:

Integer a=new Integer("23");
Integer a=new Integer(23);

System.out.println(a==b);		//false:a和b是两个不同的对象。
System.out.println(a.equals(b));	//equals覆盖了Object方法中的equals,a.equals(b)比较的是a和b中的内容。
Integer a=new Integer(127);
Integer a=new Integer(127);
System.out.println(a==b);		//false
System.out.println(a.equals(b));	//true

Integer x=new 127;
Integer y=new 127;
System.out.println(x==y);		//true,当x=128,y=128时,为false
System.out.println(x.equals(y));	//true
jdk1.5以后,自动装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间

字符串中的整数排序

思路:

1.将字符串分割为字符串数组

2.将字符串数组转化为整型数组

3.将整型数组排序

4.将排序后的整型数组按照指定的格式输出

import java.util.Arrays;
class StringIntSort {
	private static final String STRINGSPACE=" ";
	public static void main(String[] args){
		//需要排序的字符串;
		String nameStr="12 35 13 -7 90 45";
		//构建函数排序;
		nameStr =sortStringNumber(nameStr);
		System.out.println(nameStr);
	}
	
	public static String sortStringNumber(String nameStr) {
		//1.将字符串变成字符串数组
		String[] str_arr=stringToArray(nameStr);
		//2.将字符串数组变成int数组
		int[] num=toIntArray(str_arr);
		//3.对int数组排序
		mySortArray(num);
		//4.将排序后的数组变成字符串
		String temp=arrayToString(num);
		return temp;
	}
	
	//按照指定的格式输出排序后的数字。
	private static String arrayToString(int[] num) {
		StringBuilder sb=new StringBuilder();
		for(int x=0;x<num.length;x++){
			if(x!=num.length-1){
				sb.append(num[x]+" ");
			}
			else{
				sb.append(num[x]);
			}
		}
		return sb.toString();
	}

	//将整型数组排序。
	private static void mySortArray(int[] num) {
		Arrays.sort(num);
	}

	//将字符串数组转化为整型数组。
	private static int[] toIntArray(String[] str_arr) {
		int[] arr=new int[str_arr.length];
		for(int x=0;x<str_arr.length;x++){
			arr[x]=Integer.parseInt(str_arr[x]);
		}
		return arr;
	}

	//将字符串转化为字符串数组。
	public static String[] stringToArray(String nameStr) {
		String[] str_arr=nameStr.split(STRINGSPACE);
		return str_arr;
	}
}

集合类

由来:对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定,就使用容器集合进行存储。

集合的特点:

1.用于存储对象的容器;

2.集合的长度是可变的;

3.集合中不可以存储基本数据类型。

集合容器因内部的数据结构不同,有多种具体容器,但这些容器都具有某些共性方法,通过不断向上抽取,就形成了集合框架。

集合框架的顶层Collection接口的常用方法:

1.添加

 boolean add(E e) 

 确保此 collection 包含指定的元素(可选操作)。 

 boolean addAll(Collection<? extends E> c) 

将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。 

2.删除

void clear() 

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

boolean remove(Object o) 

从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 

boolean removeAll(Collection<?> c) 

移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 

3.获取

 Iterator<E> iterator() 

返回在此 collection 的元素上进行迭代的迭代器。 

boolean retainAll(Collection<?> c) 

仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 

int size() 

 返回此 collection 中的元素数。

Object[] toArray() 

返回包含此 collection 中所有元素的数组。 

<T> T[] toArray(T[] a) 

返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。 

int hashCode() 

返回此 collection 的哈希码值。

4.判断

boolean contains(Object o) 

如果此 collection 包含指定的元素,则返回 true。 

boolean containsAll(Collection<?> c)

如果此 collection 包含指定 collection 中的所有元素,则返回 true。 

boolean equals(Object o) 

比较此 collection 与指定对象是否相等。

boolean isEmpty()

如果此 collection 不包含元素,则返回 true。 

boolean isEmpty() 

 如果此 collection 不包含元素,则返回 true。 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值