当前位置:网站首页>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]
边栏推荐
- 爱可可AI前沿推介(7.29)
- 【每日SQL打卡】DAY 27丨每次访问的交易次数【难度困难-提前放出来】
- 【每日SQL打卡】DAY 23丨学生们参加各科测试的次数【难度简单】
- Network layer and transport layer restrictions
- 【每日SQL打卡】DAY 27丨列出指定时间段内所有的下单产品【难度简单】
- 微信怎么知道别人删除了你?批量检测方法(建群)
- 【每日SQL打卡】DAY 23丨向CEO汇报工作的人【难度中等】
- DNS protocol, ICMP protocol, NAT technology
- WordPress 重置密码
- Based on the flask to write a small shopping mall project
猜你喜欢

【day04】IDEA、方法

1.4, stack

365 days challenge LeetCode1000 topic - Day 043 square mathematics effectively

QML(一):自定义圆角按钮的处理

2.3 Insertion sort

2.2选择排序
Based article 】 【 learn with Rust | Rust, variables and data types

惠及6亿人 投资98亿 沿江高铁武宜段最新进展来了!

Alluxio为Presto赋能跨云的自助服务能力

Codeforces Round # 797 (Div. 3) personal answer key
随机推荐
【无标题】
SQL clock in daily DAY 21 丨 】 each post comments difficulty moderate 】 【
“祁东黄花菜”是国家地理标志保护产品吗? 蚂蚁新村7月29日答案
c语言:来实现一个小程序n子棋(已五子棋为例)
MarkDown高阶语法手册
593. 有效的正方形 : 简单几何运用题
365天挑战LeetCode1000题——Day 043 有效的正方形 数学
DAY 27 丨 daily SQL clock 】 【 every visit to the number of transactions "difficulty difficult - released in advance"
【每日SQL打卡】DAY 20丨查询结果的质量和占比【难度简单】
2.3 Insertion sort
AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
飞桨框架体验评测交流会,产品的使用体验由你来决定!
Based article 】 【 learn with Rust | Rust, variables and data types
MarkDown Advanced Syntax Manual
QML(二):设置自定义窗体
socket+websocket
Insights into the development of the enterprise live broadcast industry in 2022
MySQL高级_视图
QML(一):自定义圆角按钮的处理
DAY 26 daily SQL clock 】 【 丨 advertisement effect difficult simple 】 【