2- The Need for Generics

Jun 20, 2026 04:39 · 3:32 · English · Whisper Turbo · 2 speakers
Овај транскрипт истекава 24 Дани. Надоградња за трајно складиштење →
Приказујем само
0:03
S… Speaker 1 (2- The Need for Generics)
Before we talk about what generics are,
0:05
S… Speaker 1 (2- The Need for Generics)
let's look at the problem they try to solve.
0:07
S… Speaker 1 (2- The Need for Generics)
So in this project,
0:09
S… Speaker 1 (2- The Need for Generics)
I'm going to add a new package,
0:11
S… Speaker 1 (2- The Need for Generics)
a new package called generics.
0:14
S… Speaker 1 (2- The Need for Generics)
I'm going to write all the code for this section inside this package.
0:17
S… Speaker 1 (2- The Need for Generics)
Now let's say we want to implement a class for storing a
0:21
S… Speaker 1 (2- The Need for Generics)
list of integers.
0:22
S… Speaker 1 (2- The Need for Generics)
So in this package,
0:23
S… Speaker 1 (2- The Need for Generics)
we add a new class called list.
0:26
S… Speaker 2 (2- The Need for Generics)
In this class,
0:29
S… Speaker 1 (2- The Need for Generics)
we need an array to store our integers.
0:31
S… Speaker 1 (2- The Need for Generics)
So let's declare a private field,
0:33
S… Speaker 1 (2- The Need for Generics)
private integer array items.
0:36
S… Speaker 1 (2- The Need for Generics)
and we set it to a new int array of 10.
0:39
S… Speaker 1 (2- The Need for Generics)
Now we shouldn't hard code the length of this array here.
0:42
S… Speaker 1 (2- The Need for Generics)
It's better to add a constructor and receive the initial capacity of this
0:46
S… Speaker 1 (2- The Need for Generics)
array as a parameter.
0:47
S… Speaker 1 (2- The Need for Generics)
But let's not worry about this and instead implement a basic list class.
0:52
S… Speaker 1 (2- The Need for Generics)
So what is the next thing we need here?
0:54
S… Speaker 1 (2- The Need for Generics)
We need a method for adding a new integer to this list.
0:57
S… Speaker 1 (2- The Need for Generics)
So a public method,
0:59
S… Speaker 1 (2- The Need for Generics)
public void add,
1:00
S… Speaker 1 (2- The Need for Generics)
which gets an integer called item.
1:03
S… Speaker 1 (2- The Need for Generics)
Now here we also need a private field.
1:07
S… Speaker 1 (2- The Need for Generics)
for keeping track of the number of items in this list.
1:09
S… Speaker 1 (2- The Need for Generics)
So private int count.
1:11
S… Speaker 1 (2- The Need for Generics)
Now back to our add method,
1:14
S… Speaker 1 (2- The Need for Generics)
we type items of count and we set this to
1:18
S… Speaker 1 (2- The Need for Generics)
this new item.
1:19
S… Speaker 1 (2- The Need for Generics)
So initially count is 0 and we store this item at index 0.
1:23
S… Speaker 1 (2- The Need for Generics)
Now here we should increment count so next time we add an item it
1:27
S… Speaker 1 (2- The Need for Generics)
will be stored at index 1.
1:29
S… Speaker 1 (2- The Need for Generics)
Now we also need a method for getting an integer by
1:33
S… Speaker 1 (2- The Need for Generics)
its index.
1:33
S… Speaker 2 (2- The Need for Generics)
So
1:35
S… Speaker 1 (2- The Need for Generics)
public integer get,
1:37
S… Speaker 1 (2- The Need for Generics)
we give it an index,
1:39
S… Speaker 1 (2- The Need for Generics)
and here we return items of index.
1:43
S… Speaker 1 (2- The Need for Generics)
Now here we're not validating this index parameter,
1:46
S… Speaker 1 (2- The Need for Generics)
we're not checking to see if it's negative or greater than the number of items in
1:50
S… Speaker 1 (2- The Need for Generics)
this list.
1:51
S… Speaker 1 (2- The Need for Generics)
Let's not worry about these details and focus on an absolute basic implementation
1:55
S… Speaker 1 (2- The Need for Generics)
of a list.
1:56
S… Speaker 1 (2- The Need for Generics)
So now that we have a list,
1:58
S… Speaker 1 (2- The Need for Generics)
let's go back to our main class.
1:59
S… Speaker 1 (2- The Need for Generics)
Here we create a new list,
2:02
S… Speaker 2 (2- The Need for Generics)
new list,
2:04
S… Speaker 1 (2- The Need for Generics)
we want to import the one that is declared in this package that we just created because
2:09
S… Speaker 1 (2- The Need for Generics)
we have another class by the same name in java,
2:12
S… Speaker 1 (2- The Need for Generics)
it's declared in java .util package.
2:14
S… Speaker 1 (2- The Need for Generics)
We don't want that one.
2:15
S… Speaker 1 (2- The Need for Generics)
So, let's import our own class.
2:18
S… Speaker 2 (2- The Need for Generics)
Alright,
2:19
S… Speaker 1 (2- The Need for Generics)
now we call the add method and insert an item in this list.
2:23
S… Speaker 2 (2- The Need for Generics)
Now,
2:24
S… Speaker 1 (2- The Need for Generics)
let's say tomorrow we want to have a list of users.
2:26
S… Speaker 2 (2- The Need for Generics)
So,
2:27
S… Speaker 1 (2- The Need for Generics)
first I'm going to add a new class in this package.
2:32
S… Speaker 1 (2- The Need for Generics)
called user.
2:33
S… Speaker 1 (2- The Need for Generics)
Now in this class we could have fields like username,
2:36
S… Speaker 1 (2- The Need for Generics)
password,
2:37
S… Speaker 1 (2- The Need for Generics)
email, and so on.
2:38
S… Speaker 1 (2- The Need for Generics)
For now, let's not worry about this.
2:39
S… Speaker 1 (2- The Need for Generics)
Instead we want to have a list of users.
2:42
S… Speaker 1 (2- The Need for Generics)
Here's the problem.
2:43
S… Speaker 1 (2- The Need for Generics)
This list that we have here,
2:45
S… Speaker 1 (2- The Need for Generics)
we cannot use this class for storing users.
2:47
S… Speaker 1 (2- The Need for Generics)
We can only store integers in this class.
2:49
S… Speaker 1 (2- The Need for Generics)
So with this approach,
2:51
S… Speaker 1 (2- The Need for Generics)
we'll have to create another class called user list,
2:55
S… Speaker 2 (2- The Need for Generics)
user list,
2:57
S… Speaker 2 (2- The Need for Generics)
and
2:59
S… Speaker 2 (2- The Need for Generics)
In this class,
2:59
S… Speaker 1 (2- The Need for Generics)
we're going to have a user array.
3:01
S… Speaker 1 (2- The Need for Generics)
So private user array,
3:03
S… Speaker 1 (2- The Need for Generics)
let's import this type.
3:05
S… Speaker 1 (2- The Need for Generics)
We call it items and set it to new user array
3:10
S… Speaker 1 (2- The Need for Generics)
of 10.
3:10
S… Speaker 2 (2- The Need for Generics)
Similarly,
3:13
S… Speaker 1 (2- The Need for Generics)
we need a count field,
3:14
S… Speaker 1 (2- The Need for Generics)
so private integer count,
3:16
S… Speaker 1 (2- The Need for Generics)
then we'll have to implement our add and get methods.
3:18
S… Speaker 1 (2- The Need for Generics)
That's a lot of code duplication.
3:20
S… Speaker 1 (2- The Need for Generics)
What if in the future we want to have a list of strings?
3:23
S… Speaker 1 (2- The Need for Generics)
So this approach is very tedious and not scalable.
3:26
S… Speaker 1 (2- The Need for Generics)
In the next video,
3:27
S… Speaker 1 (2- The Need for Generics)
I'm going to show you one way to solve this problem.

This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy

❤️ Љубав STT.ai?
сажетак
Кликните на Summarise да бисте направили ВИ сажетак овог транскрипта.
Сажетак...
Питај ВИ о овом транкрипту
Питајте било шта о овом транскрипту - АИ ће наћи релевантне секције и одговор.