ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《迪哥Java教程》#13 如何在Java里创建数组 – 译学馆
最新评论 (0)

《迪哥Java教程》#13 如何在Java里创建数组

How to create Java arrays video tutorial - 013

想象一下 有人要你创建一个应用程序
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 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.
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.
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,
the sixth element which has an index of 5
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.
This can be any Java type.
在例子中是long类型 该类型后面是方括号
in this case long. The type is followed by two square braces.
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.
This statement will create a Java array with 12 longs for elements.
The statement is saying declare an array of longs,
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,
the type is the array of long. The value
值是一个特定的数组 其中有12个long类型
is a particular array with 12 longs in it.
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.
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 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
access December using
12. Well Java has no problem at compile-time.
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
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
until we get to Java classes. Java has a special API for array manipulation called Arrays.
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.
This will also give you more experience writing Java programs.
如果你有任何疑问 找到课程下方的
If you need any ideas of what to try, check the lesson page on
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 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 There’s a poll on the front page
of so you can also let me know what topic gets covered
感谢收看 下期再见
next. Thanks for watching see you in the next video!



迪哥Java致力于零基础学java 这期视频讲了如果创建数值