当前位置:网站首页>Chapter 2 Summary
Chapter 2 Summary
2022-07-29 12:01:00 【Xiaohong is working hard】
数组(一维数组为例)
1:数组的创建
JAVAThe type of the array in can be a primitive data type,也可以是引用类型.
[数据类型]数组名[];
[数据类型][]数组名;
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) {
int[] intArray = {
0,1,2,3};//指定数组长度为3,分配内存.
char charArray[] = new char[]{
'好','好','学','习'};//使用new初始化数组,Then assign values to the contents of the array.
String stringArrya[] = new String[4];//指定数组长度为4,分配内存,没有内容.
for (int a:intArray) {
System.out.println(a);
}
for (char c:charArray) {
System.out.println(c);
}
for (String s:stringArrya) {
System.out.println(s);
}
}
}
0
1
2
3
好
好
学
习
null
null
null
null
2:Array traversal and output
1).foreach遍历:
循环访问数组中的每个元素.
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) {
int[] intArray = {
0,1,2,3};//指定数组长度为3,分配内存.
for (int a:intArray) {
System.out.println(a);
}
}
}
0
1
2
3
2).简单的for循环遍历.
3).Arrays类中的toString静态方法:
public class MainClass {
public static void main(String[] args) {
int[] intArray = {
0,1,2,3};//指定数组长度为3,分配内存.
System.out.println(Arrays.toString(intArray));
}
}
[0, 1, 2, 3]
对于多维数组,toString()methods cannot be used directly,需要deepToString()A static method is required to output all elements in a multidimensional array:
toString()The method returns the memory address where the array of each dimension is stored.
deepToString()The method returns the contents of each dimension of a multidimensional array
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) {
int intArray[][] = new int[2][2];//指定数组长度为3,分配内存.
intArray[0] = new int[]{
0,1};
System.out.println(Arrays.toString(intArray));
System.out.println(Arrays.deepToString(intArray));
}
}
[[I@1540e19d, [I@677327b6]
[[0, 1], [0, 0]]
3.length();取数组长度.
4.增
1):通过for循环遍历数组,Assign a value to each element in the array.
2):ArraysThe class provides static methods that can batch add elements to an arrayfill();The input parameters are the array of elements to be added and the value to be added;It is also possible to specify the range of indices into which the array is to be added(左闭右开区间),The input parameters are the start index and end index:
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) {
String[] string = new String[4];
char[] char_one = new char[4];
Arrays.fill(string,1,3,"学");
Arrays.fill(char_one,'学');
System.out.println(Arrays.toString(string));
System.out.println(Arrays.toString(char_one));
}
}
[null, 学, 学, null]
[学, 学, 学, 学]
5.删
The array length cannot be modified,But we can refer to the method of adding an element,Rebuilds an array with elements removed,In order to achieve the purpose of deleting the specified element.
import java.util.Arrays;
public class MainClass {
private static char[] DeleteArray(char[] array,int index){
char[] newArray = new char[array.length-1];
for(int i = 0;i < newArray.length;i++){
if(i < index){
newArray[i] = array[i];
}else{
newArray[i] = array[i+1];
}
}
return newArray;
}
public static void main(String[] args) {
char[] charArray = new char[]{
'好','好','学','学','习'};
System.out.println(Arrays.toString(charArray));
charArray = DeleteArray(charArray,2);
System.out.println(Arrays.toString(charArray));
}
}
[好, 好, 学, 学, 习]
[好, 好, 学, 习]
6.查
Arrays类中提供了binarySearch()方法,Can find the position of an element in an array,Return if not found-1;
public static void main(String[] args) {
char[] charArray = new char[]{
'好','好','学','习'};
int index = Arrays.binarySearch(charArray,'学');
System.out.println(index);
}
2
7.比较
Arrays类中提供equals()方法,用来比较两个数组的内容是否相同.
public static void main(String[] args) {
char[] charArray_1 = new char[]{
'a','b','c','d'};
char[] charArray_2 = new char[]{
'a','b','c','d'};
char[] charArray_3 = new char[]{
'a','c','d'};
boolean bool = Arrays.equals(charArray_1,charArray_2);
System.out.println(bool);
bool = Arrays.equals(charArray_1,charArray_3);
System.out.println(bool);
}
true
false
8.数组复制
Arrays类中提供了数组复制的方法copyOf();
public static void main(String[] args) {
char[] charArray_1 = new char[]{
'a','b','c','d'};
char[] charArray_2 = Arrays.copyOf(charArray_1,4);
System.out.println(Arrays.toString(charArray_2));
}
[a, b, c, d]
边栏推荐
- DAY 25 daily SQL clock 】 【 丨 different sex daily score a total difficulty moderate 】 【
- 力扣sql刷题(四)
- 面试官培训课件(非常实用的企业内训课件)
- 多元宇宙:重塑新商业格局
- mysql单行,多行子查询
- CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主
- Learning with Recoverable Forgetting readings
- 【每日SQL打卡】DAY 27丨每次访问的交易次数【难度困难-提前放出来】
- DAY 20 daily SQL clock 】 【 丨 query difficulty moderate 】 【 team integral
- After connect and SQL join in on conditions and where
猜你喜欢
爱可可AI前沿推介(7.29)
面试官培训课件(非常实用的企业内训课件)
Kubernetes基本概念
2.1 Bubble sort (mercifully Sorting)
【实用工具】Image Assistant下载指定页面的所有图片
Network layer and transport layer restrictions
GDB使用详解
three.js 报错信息 RGBELoader.js:46 RGBELoader Bad File Format: bad initial token
如何对SQuAD1.1数据集进行预处理「详解版」
365天挑战LeetCode1000题——Day 043 有效的正方形 数学
随机推荐
【每日SQL打卡】DAY 27丨列出指定时间段内所有的下单产品【难度简单】
"Qidong well day lily" is the national geographical indications protection products?Ants investigation on July 29, the answer
怎么以管理员身份运行cmd?以管理员身份运行cmd方法介绍
SkiaSharp of WPF custom painting to bounce ball (case)
多元宇宙:重塑新商业格局
【每日SQL打卡】DAY 20丨查询结果的质量和占比【难度简单】
"100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
Learning with Recoverable Forgetting阅读心得
HMS Core Discovery 16 review | with tiger mound, embracing new AI "voice" state
谷歌“消灭” Cookie 计划延至 2024 年
MarkDown Advanced Syntax Manual
DAY 25 daily SQL clock 】 【 丨 different sex daily score a total difficulty moderate 】 【
Proficient in audio and video development can really do whatever you want
1.4、栈
【年中总结】创业3年,越来越穷,还是坚持架构平台
【每日SQL打卡】DAY 22丨页面推荐【难度中等】
吴恩达老师机器学习课程笔记 06 逻辑回归
DAY 26 daily SQL clock 】 【 change 丨 restaurant turnover growth difficulty moderate 】 【
【day04】IDEA, method
企业微信客户朋友圈一天可以发多少条?都有哪些限制?如何突破朋友圈可展示人数限制?