JAVA内置数据结构概述

本文介绍了JAVA中的内存单位转换,包括字节、KB、MB、GB、TB的换算。接着讲解了JAVA的基本数据类型,如整型、浮点型、逻辑型和字符型,以及它们的存储位置和转换规则。此外,还提到了BigDecimal在数值转换中的行为。最后,文章探讨了串、数组、集合和包装类的概念,以及它们在操作中的注意事项和常见方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 内存单位的转换
  2. JAVA基本数据类型和转换
  3. 串,数组,集合
  4. 包装类

一、内存单位的转换

8bit(位)=1Byte(字节)
1024Byte(字节)=1KB
1024KB=1MB
1024MB=1GB
1024GB=1TB


二、JAVA基本数据类型和转换

分类:
Java基本数据类型就8种,其他都是引用型的。
Java四类八种基本数据类型:
第一类:整型 byte short int long
第二类:浮点型 float double
第三类:逻辑型 boolean(它只有两个值可取true false)
第四类:字符型 char

存储位置:
栈中可以直接分配内存的数据是基本数据类型
引用数据类型:是数据的引用在栈中,但是他的对象在堆中

字节:
boolean 布尔型 1/8
byte 字节类型 1
char 字符型 2 一个字符能存储一个中文汉字
short 短整型 2
int 整数类型 4
float 浮点类型(单精度) 4
long 长整形 8
double 双精度类型(双精度) 8

默认:
Java中默认的整数类型是int类型,如果要定义为float型,则要在数值后加上l或L;
默认的浮点型也是双精度浮点,如果要定义为float型,则要在数值后加上f或F。

转换
基本数据类型自动转换
byte->short,char -> int -> long
float -> double
int -> float
long -> double
记住:小可转大,大转小会失去精度!


补充数据类型BigDecimal:

BigDecimal aDouble =new BigDecimal(1.22);
System.out.println("construct with a double value: " + aDouble);
BigDecimal aString = new BigDecimal("1.22");
System.out.println("construct with a String value: " + aString);

你认为输出结果会是什么呢?如果你没有认为第一个会输出1.22,那么恭喜你答对了,输出结果如下:
construct with a doublevalue:
1.2199999999999999733546474089962430298328399658203125
construct with a String value: 1.22

BigDecimal a =new BigDecimal("1.22");
System.out.println("construct with a String value: " + a);
BigDecimal b =new BigDecimal("2.22");
a.add(b);
System.out.println("aplus b is : " + a);

我们很容易会认为会输出:
construct with a Stringvalue: 1.22
a plus b is :3.44
但实际上a plus b is : 1.22


三、串、数组、集合

串的操作:
Character 是进行单个字符操作
String 字符串常量(不可变,操作后生成新的对象)
StringBuffer 动态字符串变量(线程安全)
StringBuilder 动态字符串变量(非线程安全)
[注]
String常用的方法有:+, charAt, length, subString
String 转换成StringB类可以用,appead, insert, 反之用toString方法
常用正则表达式匹配:

Pattern p = Pattern.compile("[A-z][0-9]+");
Matcher m = p.matcher(s);
ArrayList<String> al = new ArrayList<String>();
while (m.find()) {
    al.add(m.group());
}

数组的操作:
Java数组自身的操作是很少的,需要导入其他的操作包或者类
定义

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

排序和打印

int[] a = { 1, 2, 3, 4 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));

数组变成动态数组

String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]

检查数组是否包含

String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true

连接2个数组

int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

数组变成字符输出

String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
// a, b, c

数组转化成集合

Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]

数组的翻转

int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

数组元素的删除

int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));

集合操作:
集合继承关系
常用的集合框架类:
Collection:集合(一般不直接用)
List :可重复,有序
Set:不重复,无序
Map:键值对

ArrayList:效率高
Vector :效率低
Stack :后进先出

HashSet:不重复
LinkedHashSet:不重复,链表
TreeSet:不重复,有序

HashMap :无序
TreeMap :有序
HashTable:有序

集合框架的操作:
熟练的在串,数组,集合之间的转换是很有必要的。

集合中逆序的使用:

// 自己是回文则返回,自己不是回文则补全回文
public static String getHuiWen(String str) {
    if (isHuiWen(str) == true)
        return str;
    else {
        String goal = "";
        String LeftMax = getMaxSubStringFromLeft(str);
        String strSub = str.substring(LeftMax.length(), str.length());
        String sb = new StringBuilder().append(strSub).reverse().toString();
        goal = sb + str;
        return goal;
    }
}

集合迭代器的使用:

 list l = new ArrayList();
 l.add("aa");
 l.add("bb");
 l.add("cc");
 for (Iterator iter = l.iterator(); iter.hasNext();) {
  String str = (String)iter.next();
  System.out.println(str);
 }
 /*迭代器用于while循环
 Iterator iter = l.iterator();
 while(iter.hasNext()){
  String str = (String) iter.next();
  System.out.println(str);
 }
 */

四、包装类
包装类的主要:
Java是面向对象的,但是基本数据类型不是对象,做成对象后才能传递到某一些方法里。

包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类Number的子类,所有的包装类如下:
byte
Byte
boolean
Boolean
short
Short
char
Character
int
Integer
long
Long
float
Float
double
Double

和基本数据类型一样使用:

Integer a = 10;
a = a + 10;
System.out.println(a);

基本类型和包装类转换

int n = 10;
Integer in = new Integer(100);
Integer in1 = new Integer(n);
int m = in.intValue();

数字字符串变成数字

String s = “123”;
int n = Integer.parseInt(s);

给定进制的转换

//将字符串”120”按照十进制转换为int,则结果为120
int n = Integer.parseInt(“120”,10);
//将字符串”12”按照十六进制转换为int,则结果为18
int n = Integer.parseInt(“12”,16);
//将字符串”ff”按照十六进制转换为int,则结果为255
int n = Integer.parseInt(“ff”,16);

数字转化成数字字符

int m = 1000;
String s = Integer.toString(m);
//或者
String s = m + "";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值