学习知多少:java篇
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard学苑。今天小编为大家带来“学习知多少:java篇”,欢迎您的访问。
Share interests, spread happiness, increase knowledge, and leave a good legacy!
Dear you, this is The LearningYard Academy. Today Xiaobian brings you "Learn how much to know :Java chapter”, welcome your visit.
一、思维导图
一、 Mind mapping
二、数组
Second, arrays
1.动态初始化: 数据类型 [] 数组名称 = new 数据类型 [长度];
1. Dynamic initialization: data type [] array name = new data type [length];
eg: int [] arr = new int [10];然后通过下标索引为每个元素赋值。
eg: int [] arr = new int [10]; Each element is then assigned a value by a subscript index.
注:new关键字的含义是在堆上开辟一块新的空间。只要有new关键字的出现,就意味着有新空间的开辟。
Note: The meaning of the new keyword is to open up a new space on the heap. As long as the new keyword appears, it means that there is a new space to open up.
2.静态初始化:在定义数组的同时设置元素的值。
2. Static initialization: Set the value of the elements while defining the array.
简化格式:
数据类型 [] 数组名称 = {值1,值2,…};
Simplified format:
Data type [] array name = {value1, value2,...};
完整格式:
数据类型 [] 数组名称 = new 数据类型 [] {值1,值2,…};
Full format:
Data Type [] Array Name = New Data Type [] {Value 1, Value 2,...};
3.匿名数组
3. Anonymous arrays
利用完整格式,可以方便使用匿名数组。
The full format makes it easy to work with anonymous arrays.
匿名数组就是没有名称的数组,本质是一块没有栈内存指向的堆内存。只能使用一次,用完就被回收了。
An anonymous array is an array without a name, which is essentially a piece of heap memory that has no stack memory to point to. It can only be used once, and it is recycled when it is used up.
注:其中列个数可以省略不写,因为Java中的二维数组存在不规则矩阵这样的数组,因此写了也没有多大意义。
Note: The number of columns can be omitted and not written, because there is an array such as an irregular matrix in two-dimensional arrays in Java, so it does not make much sense to write.
eg:不规则二维数组的使用:
eg: Use of irregular two-dimensional arrays:
输出结果:
Output result:
在C语言中二维数组每一行的个数都是相同的。
In C, two-dimensional arrays have the same number of rows per row.
利用增强型for循环(foreach循环)打印多维数组。
Use the enhanced for loop (foreach) to print multidimensional arrays。
格式:for(part1:part2){part3}
part2 :一个数组对象
part1:定义一个变量,变量的类型是数组中元素的类型。
part3:循环体
原理:将part2 数组中的元素值拷贝一份逐一赋给part1,然后利用part3循环体将值打印出来。因此该循环只能作为输出不能修改数组中元素的值。因为它仅是一份值得拷贝。
Format: for(part1:part2){part3}
part2: An array object
part1: Define a variable, the type of the variable is the type of the elements in the array.
Part 3: Circular bodies
Principle: Copy the element values in the part2 array to part1 one by one, and then use the part3 loop body to print the values. Therefore, the loop can only be used as output and cannot modify the values of the elements in the array. Because it"s just a copy worth copying.
利用增强型for循环输出多维数组和一维数组的本质思想是一样的。
The essential idea of using enhanced for loops to output multidimensional arrays and one-dimensional arrays is the same.
以二维数组为例:
Take a two-dimensional array as an example:
首先二维数组的每一个元素类型都是一维数组,因此第一个增强型for循环的第一个局部变量为一维数组类型,第二部分是二维数组名。但不能利用循环体直接将一维数组整体输出。因此需要再嵌套一个增强型for循环,此时的for循环里的局部变量类型为外部增强型for循环里面的一维数组元素的类型,第二部分是第一层增强型for循环里的一维数组本身。然后循环体中依次输出这个局部变量即OK了!
First of all, each element type of a two-dimensional array is a one-dimensional array, so the first local variable of the first enhanced for loop is the one-dimensional array type, and the second part is the two-dimensional array name. However, you cannot use the loop body to directly output the one-dimensional array as a whole. Therefore, it is necessary to nest an enhanced for loop, at this time, the local variable type in the for loop is the type of the one-dimensional array element in the outer enhanced for loop, and the second part is the one-dimensional array itself in the first layer of the enhanced for loop. Then output this local variable in turn in the loop body, that is, OK!
今天的分享就到这里了。如果您对今天的文章有独特的想法,欢迎给我们留言,让我们相约明天。祝您今天过得开心快乐!
That"s all for today"s sharing.If you have a unique idea for today’s article,please leave us a message,and let us meet tomorrow.I wish you a happy day !
本文由learningyard新学苑原创,如有侵权,请联系我们!
翻译来源于谷歌翻译
部分来源于
百度文库
清华大学出版《java程序设计基础》
编辑&排版|百味
审核|闫庆红