Εμφάνιση μόνο
0:03
S… Speaker 2 (3- The Need for Iterables)
As I told you in the last video,
0:05
S… Speaker 2 (3- The Need for Iterables)
the iterable interface is at the top of our hierarchy.
0:08
S… Speaker 1 (3- The Need for Iterables)
Now,
0:09
S… Speaker 2 (3- The Need for Iterables)
more accurately,
0:09
S… Speaker 2 (3- The Need for Iterables)
this interface is not part of the collections framework.
0:12
S… Speaker 2 (3- The Need for Iterables)
In fact,
0:13
S… Speaker 2 (3- The Need for Iterables)
it's part of the java .lang package.
0:15
S… Speaker 2 (3- The Need for Iterables)
So it's one of the fundamental interfaces in Java and represents
0:19
S… Speaker 2 (3- The Need for Iterables)
an object that we can iterate or loop over.
0:22
S… Speaker 1 (3- The Need for Iterables)
Now,
0:23
S… Speaker 2 (3- The Need for Iterables)
before we look at the specifics of this interface,
0:25
S… Speaker 2 (3- The Need for Iterables)
let's talk about the problem it tries to solve.
0:27
S… Speaker 2 (3- The Need for Iterables)
So here in the main method,
0:29
S… Speaker 2 (3- The Need for Iterables)
I'm going to create a generic list.
0:31
S… Speaker 2 (3- The Need for Iterables)
new generic list of strings.
0:34
S… Speaker 2 (3- The Need for Iterables)
Let's add a couple of objects here.
0:37
S… Speaker 2 (3- The Need for Iterables)
So add a and b.
0:39
S… Speaker 2 (3- The Need for Iterables)
Now we want to iterate over this list and print each item.
0:43
S… Speaker 2 (3- The Need for Iterables)
So we write a for each loop for item in list.
0:47
S… Speaker 1 (3- The Need for Iterables)
Look,
0:48
S… Speaker 2 (3- The Need for Iterables)
we have a compilation error here.
0:49
S… Speaker 2 (3- The Need for Iterables)
For each not applicable to type generic list.
0:52
S… Speaker 1 (3- The Need for Iterables)
Now,
0:53
S… Speaker 2 (3- The Need for Iterables)
one way to solve this problem is to go back to the generic list class and
0:58
S… Speaker 2 (3- The Need for Iterables)
make this private field public.
1:01
S… Speaker 2 (3- The Need for Iterables)
then we'll be able to access the items array from outside of this class.
1:05
S… Speaker 2 (3- The Need for Iterables)
So here in the main method,
1:07
S… Speaker 2 (3- The Need for Iterables)
we can type list .items now we can iterate over
1:11
S… Speaker 2 (3- The Need for Iterables)
this array and print each item.
1:13
S… Speaker 2 (3- The Need for Iterables)
Now the problem with this approach is that we have exposed the internal
1:18
S… Speaker 2 (3- The Need for Iterables)
implementation of this class to the outside.
1:20
S… Speaker 2 (3- The Need for Iterables)
So if we decide to change this implementation in the future and
1:24
S… Speaker 2 (3- The Need for Iterables)
replace this array with a different data structure
1:28
S… Speaker 2 (3- The Need for Iterables)
All the code that is dependent on this field will have to be changed.
1:31
S… Speaker 2 (3- The Need for Iterables)
As a metaphor,
1:32
S… Speaker 2 (3- The Need for Iterables)
think of the remote control of your TV.
1:34
S… Speaker 2 (3- The Need for Iterables)
This remote control has a few simple buttons for you to work with,
1:38
S… Speaker 2 (3- The Need for Iterables)
but on the inside,
1:39
S… Speaker 2 (3- The Need for Iterables)
it has a complex logic board.
1:41
S… Speaker 2 (3- The Need for Iterables)
That's the implementation detail.
1:43
S… Speaker 1 (3- The Need for Iterables)
Now,
1:44
S… Speaker 2 (3- The Need for Iterables)
if tomorrow the manufacturing company decides to change this internal implementation,
1:48
S… Speaker 2 (3- The Need for Iterables)
you are not going to get affected.
1:50
S… Speaker 2 (3- The Need for Iterables)
If they replace a transistor with an advanced model,
1:53
S… Speaker 2 (3- The Need for Iterables)
you can still use the same buttons you're familiar with.
1:56
S… Speaker 2 (3- The Need for Iterables)
So changing the internal implementation of a remote control should not
2:00
S… Speaker 2 (3- The Need for Iterables)
impact its public interface.
2:02
S… Speaker 2 (3- The Need for Iterables)
In object -oriented programming,
2:04
S… Speaker 2 (3- The Need for Iterables)
we should design our classes in the same way.
2:06
S… Speaker 1 (3- The Need for Iterables)
Again,
2:07
S… Speaker 2 (3- The Need for Iterables)
I talked about this concept a lot in the second part of this series.
2:10
S… Speaker 2 (3- The Need for Iterables)
So back to our generic list,
2:12
S… Speaker 2 (3- The Need for Iterables)
we should not expose the items filled to the outside because this is the implementation
2:16
S… Speaker 2 (3- The Need for Iterables)
detail.
2:17
S… Speaker 2 (3- The Need for Iterables)
Let me show you the problem with this code.
2:18
S… Speaker 2 (3- The Need for Iterables)
With this implementation,
2:20
S… Speaker 2 (3- The Need for Iterables)
we can go back to our main method and write code like this.
2:23
S… Speaker 2 (3- The Need for Iterables)
List that items of zero,
2:26
S… Speaker 2 (3- The Need for Iterables)
we set this to a string like a,
2:28
S… Speaker 2 (3- The Need for Iterables)
and then we can print list .items .links.
2:31
S… Speaker 1 (3- The Need for Iterables)
Now,
2:32
S… Speaker 2 (3- The Need for Iterables)
what is the problem here?
2:33
S… Speaker 2 (3- The Need for Iterables)
Well, there is no problem right now,
2:35
S… Speaker 2 (3- The Need for Iterables)
but if tomorrow we decide to replace this array with
2:39
S… Speaker 2 (3- The Need for Iterables)
an ArrayList object,
2:40
S… Speaker 2 (3- The Need for Iterables)
our code is going to break.
2:42
S… Speaker 2 (3- The Need for Iterables)
Let me show you.
2:43
S… Speaker 1 (3- The Need for Iterables)
So,
2:43
S… Speaker 2 (3- The Need for Iterables)
we type ArrayList of t,
2:47
S… Speaker 2 (3- The Need for Iterables)
this is a class declared in the collections framework,
2:49
S… Speaker 2 (3- The Need for Iterables)
and it's an implementation of a dynamic array.
2:52
S… Speaker 2 (3- The Need for Iterables)
We're going to look at it soon.
2:53
S… Speaker 2 (3- The Need for Iterables)
So let's create an ArrayList object here,
2:56
S… Speaker 2 (3- The Need for Iterables)
new array list.
2:57
S… Speaker 2 (3- The Need for Iterables)
Now back to the main method.
3:00
S… Speaker 2 (3- The Need for Iterables)
See what happened?
3:02
S… Speaker 2 (3- The Need for Iterables)
Our code broke.
3:04
S… Speaker 2 (3- The Need for Iterables)
Because the array list class does not support the square bracket
3:08
S… Speaker 2 (3- The Need for Iterables)
syntax for accessing an object by its index.
3:11
S… Speaker 1 (3- The Need for Iterables)
Also,
3:12
S… Speaker 2 (3- The Need for Iterables)
it doesn't have a field called length.
3:16
S… Speaker 2 (3- The Need for Iterables)
So a simple change in the implementation detail of our generic list
3:20
S… Speaker 2 (3- The Need for Iterables)
resulted in two breaking changes
3:23
S… Speaker 2 (3- The Need for Iterables)
in our main method.
3:24
S… Speaker 2 (3- The Need for Iterables)
Now this is a very simple program,
3:26
S… Speaker 2 (3- The Need for Iterables)
but in a real application,
3:27
S… Speaker 2 (3- The Need for Iterables)
we could have hundreds of references to our generic list.
3:30
S… Speaker 2 (3- The Need for Iterables)
So if you change the implementation detail,
3:32
S… Speaker 2 (3- The Need for Iterables)
then we'll have to go back and fix all those hundreds of places that are broken.
3:36
S… Speaker 2 (3- The Need for Iterables)
So here's the question,
3:38
S… Speaker 2 (3- The Need for Iterables)
how can we iterate over a list without knowing anything about its
3:42
S… Speaker 2 (3- The Need for Iterables)
internal implementation?
3:43
S… Speaker 2 (3- The Need for Iterables)
That is where the iterable interface comes to the rescue.
3:47
S… Speaker 2 (3- The Need for Iterables)
In the next video,
3:48
S… Speaker 2 (3- The Need for Iterables)
I'm going to show you how to solve this problem using the iterable interface.

Αυτό το αντίγραφο δημιουργήθηκε από τον AI (αυτόματη αναγνώριση ομιλίας). Μπορεί να περιέχει σφάλματα ~ επαλήθευση ενάντια στον αρχικό ήχο για κρίσιμη χρήση. Πολιτική AI

❤️ Σου αρέσει το STT.ai; Πες το στους φίλους σου!
Περίληψη
Κάντε κλικ στο Summarize για να δημιουργήσετε μια περίληψη AI αυτής της μεταγραφής.
Συνοψίζοντας...
Ρωτήστε τον Αλ γι' αυτό το σενάριο.
Ρωτήστε οτιδήποτε σχετικά με αυτό το αντίγραφο, το AI θα βρει σχετικές ενότητες και θα απαντήσει.