想象一下 有人要你创建一个应用程序
Imagine someone has asked you to create an application
报告你的城市的平均 高 中 低温度
that reports the average, high, medium and low temperature for your city.
您可以为每个月创建三个变量 这样就需要记录36个变量
You can create three variables for each month. This would give you 36 variables to keep track of.
因为一共有三种温度 十二个月
That’s three temperatures, times twelve months.
确定四月份的平均低温意味着
Determining the average low temperature for April would mean
你需要有特殊的代码来匹配所选择的月份的三个变量
you’d need to have special code to match the selected month with the three variables for that month.
这看起来好像有大量工作
That sounds like a lot of work.
幸运的是 我想要展示一个更好的方式给你看
Luckily there’s a much better way that I want to show you.
Java具有称为数组的数据结构
Java has a data structure called arrays.
这节课的目标是
Here your goals for this lesson are
你会了解数组是什么 怎么表示以及怎么使用
you want to understand what an array is, and what it represents and what it looks like.
你需要知道如何在Java代码中创建数组
You need to know how to create arrays in your Java code.
并且创建了一个数组后 你会想知道如何获得数组里面单独的元素
And once you’ve created one, you’ll want to know how to get the individual elements in an array.
复制数组是很重要的知识点 所以
Its important to know how to copy arrays so I’m
我将先讲如何复制数组 这里有一份关于数组使用场合的清单
going to teach you how to copy arrays and there’s a whole list of tasks you can do with an array,
任务中提供了很多实例
and job has provided many of them for you.
你可以在java.util.Arrays中了解使用这些工具
You want to know how to use these tools in java.util.Arrays.
在内存中 一个数组
In memory an array
占据一个内存区域 您可以将每个内存地址视为一个邮箱
occupies a region of memory. You can think of each memory location as a mailbox,
每个邮箱都有自己的地址
each with its own address.
在上一课中 你已经学习了如何在变量中存储单个值
In a previous lesson you learned how to store a single value in a variable.
该变量被保存在单个内存地址中
The variable is held in a single memory location.
在天气示例中 我们不想创建36个不同的变量
For our weather example, we don’t want to create 36 distinct variables.
当我们需要保存一个更大的有序变量而且所有的变量都是相同类型
When we need to hold a larger ordered group of variables, all the same type,
我们只需要用数值就可以解决 使用一个数组 你可以创建一个任意类型的数组
we use an array.You can create an array any type including an array of arrays.
包括数组的数组 我们在最后会说到
We’ll look at that later, but
但先来看一下简单的数组 这是一个由八个元素组成的数组
first lets look at a simple array. Here’s an array of eight elements.
数组中的每个元素都与上一个元素相邻
Each element in an array is next to the previous element,
从最小到最大的地址排序
ordered from the least to greatest address.
其中一个关键特征就是它是固定长度的
One of the key characteristics of an array is that it’s a fixed-length.
如果指定数组8个元素 像这样 当我们尝试添加第九个元素时 它不会增长
If you specify an array to hold 8 values, like this one, it will not grow when we try to add a ninth element.
对于具有八个元素的数组 每个元素的索引从
For an array with eight elements, the index for each element is from
0到7 第一个元素总是从0开始
0 to 7. The first element always starts at zero.
这就是为什么你总是看到这样的程序员笑话:
That’s why you always see programmer jokes like this.
记住了吗 第一个元素是0
Get it? First element is 0.
难道不好笑吗 呵呵 再解释一下
Isn’t that hilarious? Ha Ha. Alright just to make things clear,
如果我们使用这样的随机变量填充数组
if we populate our array with random variables like this,
则第六个元素的索引为5
the sixth element which has an index of 5
它的值是67
has the value of 67.
我们来看一下声明数组所需的Java语句 它以Java原有类型开始
Let’s look at the Java statement required to declare an array. It starts with the Java type.
可以是任何Java类型
This can be any Java type.
在例子中是long类型 该类型后面是方括号
in this case long. The type is followed by two square braces.
方括号告诉Java你想要一个这种类型的数组
The braces tell Java that you want an array
变量名称和其他变量名类似
of this type. Variable name is like any other variable name.
你可以随意命名 尽量起个好名字!
You can call it whatever you want. Make a good name!
既然我们在创建温度app 就将它命名为highTemperatures吧(高温)
Since we’re starting with the temperature app, let’s call it high temperatures.
然后 你可以选择如何创建数组
Then you have a choice of how you create your array.
占位符或文字 第一种方法是使用“new”关键字
Place holders or literals. The first way is to use a “new” keyword.
它看起来像这样 我们再次指定数组类型 但这次
That looks like this. We specify the array type again, but this time
我们包括元素的数量 这是真正的数字
we include the number of elements. This is the true number.
如果你想要10 就放10
If you want 10, put 10.
这个语句将创建一个有12个long类型为元素的Java数组
This statement will create a Java array with 12 longs for elements.
这语句是声明一个long型数组
The statement is saying declare an array of longs,
命名为’highTemperatures’
call it highTemperatures,
然后创建一个12个long类型为元素的数组 并将其分配到’highTemperatures’
then create an array of 12 longs, and assign it to high temperatures.
这就像创建一个普通变量并将其分配给一个值时
It’s like when you create a normal variable and assign it to a value,
类型是long的数组
the type is the array of long. The value
值是一个特定的数组 其中有12个long类型
is a particular array with 12 longs in it.
每个元素的索引将从0到1
The index to each element will run from 0 to 11.
你也可以声明数组之后打破声明再创造
You can also break the declaration in creation up.
这个方法在你已经声明数组后想创建自己的数组很有用
This is can be useful if you want to create your array with some time after declaring the array
如果你预先知道每个元素是什么 你也可以
If you know in advance what values for each element will be, you can also create the
直接将元素写出来 像这样
array from a list of literals. That looks like this.
在这种情况下 你不需要告诉Java数组中有多少元素
In this case you don’t need to tell Java how many elements are in your array.
Java可以弄清楚 这里 它将创建一个大小为2的数组
Java can figure it out. For this it would create an array of size 2.
我们来到Java编辑器 编写我们的三个温度数组
Let’s go to the Java editor and write our three temperature arrays.
我们将为每个温度范围创建一个数组:高温 平均温度和低温
We’ll create one for each temperature range: high, average,
我们从往常的起点开始吧
and low. Let’s start in usual starting place.
我们要做的就是创建三个数组
So what we want to do is create three arrays,
每个数组有12个元素 一个月就是一个元素
each has 12 elements. One for each month.
第一个存储每月低点温度
The first to store the monthly low temperatures.
第二个数组储存月平均气温
Second stores the monthly average temperatures.
最后数组存储每月高温
And the final array stores the monthly high temperatures.
现在让我们打印5月份的高、中、低温 五月是第五个月
Now let’s print out the high, medium, and low temperatures for May. May is the fifth month,
但记住数组是零为基础的
but remember arrays are zero-based.
意味着从0开始
That means they start at 0.
所以5月的索引是4 我们运行
So the index value would be 4 for May. We run it,
我们得到五月的值 你需要确保你的大括号的位置正确
and we get the values for May. You need to make sure you have the braces in place.
如果你在数组中忘记了一个括号 那就会是这个样子
Here’s what it looks like if you forget a brace in your array.
如果你忘记了第一个括号 你会得到如下报错:
If you forget the first brace, you get the error
int不能转换为int数组
“int cannot be converted to an int array”.
如果你忘了关闭后面的括号 它会说
If you forget the closing brace, it says
你忘记你的括号 当你访问不存在的值得时候会发生什么呢
you forgot your brace. What happens if you try to
例如 假设你忘了这是0开始的
access a non-existent value. For example, assume you forgot that’ it’s 0 based and try to
并要使用12月份的数据
access December using
那么Java在编译时没有问题的
12. Well Java has no problem at compile-time.
12是可得的索引即使它不是这个数组的有效索引
12 is valid index even though it’s not a valid index for this array.
所以没有编译器报错
So no compiler errors.
我们运行它 会得到一个运行时错误
We run it and we get a runtime error
运行时报错:java.lang.ArrayIndexOfBoundsException
The runtime error says java.lang.ArrayIndexOfBoundsException
如果你仔细看 它甚至可以显示你尝试使用的值
If you look close, it even shows you the value tried to use.
在这个例子中是12 我接下来要将的事情是如何复制的数组
In this case 12. Next thing I want to show you is how to copy an array.
现在你可能会认为 我们可以仅通过把新的数组设置为
Now you might think that we can just copy the array by setting the new array to
和已经存在的数组相等就行了
the existing array using equals.
这还挺有道理 但是你错了
And that kind of a makes sense, but you’d be wrong.
看看会发生什么 如果我们复制一个数组
Look what happens if we copy an array,
第五个值设为42 现在让我们打印出第一数组
set the fifth value to 42. Now lets print out the first array. This is a
这是一个特殊的调用来打印数组
special call to print arrays.
暂时不用知道它的原理 这个我们在之后的课程中会讲到
Just know that it works when we use it. Methods are coming in later lessons.
我们看到第一个数组也改变了
So the first array change too.
这并不是我们想要的结果
That isn’t what we wanted. The reason is
原因是我复制了一个引用 这个我们在之后也会讲到
we copied a reference. That a topic that we’re gonna cover later.
在我们的邮箱比喻中我们说了邮箱拥有
In our mailbox analogy we said a mailbox holds
一个类型 这个类型也是唯一的
one type and one type only. The type that we’re holding
我们看到的是数组的引用 而不是数组里面的值
is an array reference, not the values in the array.
只是邮箱的地址
Just the address to our mailbox.
因此 我们定义了一个数组引用 然后创建一个数组并将我们
So we defined an array reference, then created an array and said
第一个引用指向它 然后我们定义第二个引用
our first reference points to that. Then we defined a second reference
然后指向相同的数组
and pointed at the same array. So
所以真正我们是怎么复制数组的?我们能复制每个元素到一个新的数组
how do we copy the arrays? We could copy each element over into a new array
但是很快会带来新的烦恼
but that would get annoying real quick.
如果我们的数组里有数以百万的元素
If we had millions in our array, even if we wanted to type each copy,
纵然我们想复制每一个元素 那也会是无休止的任务 我们需要一个更好地方法
that would take forever. There’s gotta be a better way.
这里有一个更好地方法 我们将使用你们现在还不太理解的东西–Java类
There’s a better way. We’re going to use more stuff we don’t completely understand
Java有一个特殊的API用于数组操作–Arrays
until we get to Java classes. Java has a special API for array manipulation called Arrays.
这个API有一个调用可以更高效地以数据块为单位来操作数组
It has a call that moves arrays around in blocks, which is more efficient.
当我们想要复制数组
When we want to copy
我们只要调用复制方法即可
an array, we just use this copy call.
让我们来试试 回到我们的问题
So let’s try that. Let’s go back to our problem.
我们有两个数组 然后我们来试着复制
We have two arrays, and we tried making a copy.
我们改变声明来声明数组
We’ll change the assignment to call
用copyOf来代替 我们重新运行
Arrays.copyOf instead. We run it again
这次我们改变第二个数组的元素不再影响第一个数组了
and look! Changing an element in the second array does not affect the first
前面我们说可以学到多维数组
Now I said earlier you can have a multi-dimensional array.
就像这样是个有序的矩阵
That’s really just a matrix of values ordered like this.
要声明它 你只要多加一个方括号就行了
To declare you just add another set of square braces.
就像这样 让我们在代码中创建一个数组的数组吧
That looks like this. Let’s go create a multi-dimensional array in code.
我们使用这样的语法来创建一个方阵
We create a square array using the following syntax.
真的没有什么特别的
There’s really no magic to this.
我们可以创建任何维数的数组
We can create arrays of any dimension.
只需不断添加更多的括号就可以
We just keep adding more braces to the creation.
我们同样可以创建多维的文字数组
We can create multi-dimensional literal arrays too.
让我们将我们的温度数组声明为一个多维数组吧
Lets alter our temperatures array to be one multi-dimensional array.
我们像这样修改数组
We’ll modify our array literal like this.
我们正在做的就是把数组当做元素
What we’re doing is putting arrays as the elements in our array and then those arrays have ints.
这样我们就可以用一个数组来存储我们的温度了
So now we can use one array to store our temperatures.
0指向低温 1指向平均温度 2指向高温
0 will be the index for low temperatures, 1 will be the average temperatures,
还有很多操作数组的方式
and 2 will be high temperatures. There’s many other things you can do to manipulate arrays.
你可以在实践中体验不同的调用 看看他们到底有什么用
I encourage you to go experiment with the different calls and see how they affect the array.
这同样会丰富你写Java程序的经验
This will also give you more experience writing Java programs.
如果你有任何疑问 找到课程下方的DeegeU.com
If you need any ideas of what to try, check the lesson page on DeegeU.com.
在评论中写下你的困惑
Let me know if you run into any issues down on the comments,
让我们下期再见
and I’ll see you next lesson.
嘿 感谢观看这期视频
Hey! Thanks for watching the video.
如果你要检测这节课的学习效果 官网上有个小测试
There is a quick quiz for this on DeegeU.com if you’d like to gauge how much you learned.
如果你喜欢这个系列的视频 请让我知道 可以为我点赞
If you like the videos you are seeing, please let me know by liking the video and hitting the subscribe button
DeegeU频道 我会非常感激的 如果你有任何疑惑
to the DeegeU channel on YouTube. I’d really appreciate that. If you have concerns or
或者问题 请在下方的评论区或者
questions, please leave them in the comments below
官网的评论区留言 官网首页还有一个投票
or on DeegeU.com. There’s a poll on the front page
是关于下节课将什么的
of DeegeU.com so you can also let me know what topic gets covered
感谢收看 下期再见
next. Thanks for watching see you in the next video!
