declare

Java List Tutorial



Sharing buttons:

the java.util list interface is part of

the Java collections API the list

interface represents an ordered list of

objects and by ordered means that the

list can be iterated in a certain order

and it also means that each object can

be accessed at a certain position in the

list in the example I've shown here we

the list contains four objects and if

you iterate the list you will start with

the first object then you will access

the second then the third and then the

fourth you can also access each of these

objects directly by passing the index to

the list interface method get' method

and we will get back to that later

but then you can jump directly to a pick

number three in the list or two or

whatever the four most commonly seen

implementations of the list interface is

the ArrayList the linked list the vector

and the stack in modern applications you

will you will not use the vector very

much the vector implementation very much

it is basically similar to the ArrayList

except that all methods in the vector

class are synchronized and this might be

useful if you were making a

multi-threaded application but there are

still many other ways to as your

exclusive access to a vector then

synchronizing all the methods so in

practice the vector class is not very

much used anymore the ArrayList is a

list of objects that are stored in an

array or the references to the OP to the

object are stored in an array and that

makes it very fast to access a given

object because you can jump directly to

a certain index in the array and access

it there the disadvantage of an

ArrayList is that you may not know ahead

of time how many elements you want into

the list and that means you may have to

allocate an array of say 25 or 100

elements or objects but you may only

have three or four elements in the array

list

and so you might waste a little bit of

space and another disadvantage is that

once you reach the capacity you see in

this example I have only space for five

objects in the array once you reach the

capacity of the ArrayList it has to

expand and that means that you have to

create a new array that is bigger than

the old array and copy all the object

references to the new array the linked

list does not have this disadvantage

because it consists of linked list

elements that point to each other and

that means that in order to expand the

list you can just create one or the list

just created one new list element at the

bottom here and put a pointer from the

previous end of the list element to this

new end of the list element so the

linked list has the advantage that it

never uses more memory than you actually

need but it has the disadvantage that

it's slow to access elements in because

in order to find say element number

three you have to first go to element

number one then number two then number

three in the list so you have to find

the first element and you follow the

reference to the next element and then

from the next element follow the

reference to the next element and so on

and so forth that makes searching in a

linked list much slower than in an

ArrayList in practice because of how

modern computer hardware works with the

cache in its etc where CPUs are very

fast at searching through sequential

sequences or areas of memory the

ArrayList is by far the fastest to use

and in most cases you can live with a

little bit of extra wasted space in case

you know that the space that is waste is

by allocating more space in the array

that is actually needed the stack class

also implements the list interface but

since you normally use a stack slightly

different than you do a list I will

leave it out of this video now let's

jump in and see some code examples of

how to use the Java util list interface

first I've created a single class here

with a main

that in and then I will create a list

here my list and and I will create a new

ArrayList that is how it's done and as

you can see I have to import both

ArrayList and the list interface and you

can see the import are shown up here at

the top of the the code window and this

is all it takes to create an instance of

an ArrayList

and assign it to a variable of type list

and since the ArrayList implements the

list interface this assignment is

perfectly valid now let's add some

elements to the list you add elements to

the list by calling the the list add

method and this is how it looks now the

list is untyped and that means that it

contains objects

I mean elements or objects of type

object and that means that we can put

any kind of object into it as you can

see here I've put a string into it I can

put more elements or more objects into

the list simply by calling the add

method several times the add method adds

the object that is passed to it as

parameter to the list and it adds it at

the end of the list so that means this

time the first here adds this object as

number zero in the list the the list is

zero indexed so even though it says

object one it actually has index zero

the next object will be added with index

one and the next will be added with

index two if I want to remove an element

from the list or an object from the list

I can call them the list remove method

and the remove method can either take an

object to remove and that means that you

have to it has to be

reference to an object that's already in

the list or simply an index and in this

case I will just use an index and I will

pass index to and that means this is the

last element in the list here the object

3 string that will be removed from the

list it is possible to access the

element of the list using their index

and you use the get method of the list

interface to do that and for instance if

I want to access element the first very

first element in the list I can simply

call get with an index of 0 that is the

first element in the list and of course

I have to assign it to some variable so

that is how it looks in order to get

this to a string because I know that I

have only put strings into the list I

would have to cast it right I will have

to cast whatever get the get method

returns like this to obtain the size of

a list you simply call the size method

like this size returns the number of

objects in the list at any given time

there are three ways to iterate a list

and by iterating I mean accessing the

element to one at a time starting with

the first one and then finishing with

the last element in the list

the first method here is to use an

iterator and this is the old way of

iterating a list and you obtain an

iterator from a list simply by calling

the iterator method on the list and the

iterator has a hasnext method and this

will return true as long as the

has more elements in the list to iterate

so this is how you can create a while

loop that iterates through all the

elements in the list and to obtain the

next element in the list through the

iterator you simply called iterator next

method and then returns the next object

in the list and it moves also it also

moves the pointer to where the next

element in the list so that the next

time you call iterator next it will call

again it will return the next element

and then the next element after that and

the next element after that so once this

while loop finishes this loop here this

while loop will have executed one time

for each object in the list and this

next variable here will have been bound

to each element in the list one at a

time one time for one update for each

iteration and the second way to iterate

a list is to use the new for loop that

was added in Java five and you do like

this and that's pretty much it that's

all you need to right first of all you

tell the for loop

what list or collection in general to

iterate and then you say well each

element or each object in the list as I

iterate it should be bound to this

variable and that means that in here for

easy duration the next variable is bound

to in the first iteration this object

one your string and then in the second

iteration the object to string and in

the third it will be bound to the three

string so that is the second way the

last way I will show you is to simply

iterate through the list using a

standard for loop I will start with

index I equals zero and as long as I is

less than the size of the list

I will increment I like this then I can

access each element in the list simply

by calling gate with a as parameter now

this version here is every bit as fast

as this and this if you're using an

ArrayList there is no difference however

if you're using a linked list this is a

very slow way of iterating a list

because let's say you in iteration 10

then in order to get element number 10

you first have to go to element number 1

then number 2 3 4 5 6 7 8 9 and then 10

that is how you obtain element n in a

linked list so the further into the you

know the the more iterations you perform

the longer the closer towards the end of

the list in the link that you get the

slower this way of iterating the list

becomes but finally list it's just fine

the last thing I want to show you is how

to empty a list and you do so by calling

the clear method on the list

once the clear method has been executed

all elements will be removed from the

list and exactly how the list

implementation cleans up its internal

storage it depends on the list

implementation actually there is just

one more thing that I want you to show

you what to do and that is to how to

create a generic list or list with the

type sign on it by default when you

create a list you can add any kind of

object to it look at this I can add a

new integer to the list right like this

all objects can go into such a list

because it's untyped but now I can set a

type on the list i declare here with

inside the diamond brackets that i will

only allow string objects to go into the

list this is a generic list now and it

has the type string the generic type

string

now you can see these three lines are

still valid but all of a sudden it's no

longer valid to add in an integer to the

list so I can now delete it now that the

list is generic if I want to get an

object out of the list string my string

equals my list get 0 like this you see

normally I would have to cast the

element obtained from the list to a

string but this is not necessary because

the list is typed and the compiler knows

that only strings can come out of the

list so the get method can only return a

list so typing is typecasting is no

longer necessary when you retrieve

elements from the list and then there's

they are some other advantages when you

are iterating the list for instance you

can create an iterator and this iterator

too can be typed and it looks like this

and and now the iteration looks the same

but you can see now the iterator that

next method returns string objects

because the list is typed and then

normally I would also have to do a cast

here to a string but this is also no

longer necessary so the code gets a

little bit shorter and there's also

another way to iterate the list let me

just show you that you can use the the

for each loop so I can write it like

this for string next my list like this

and then for each iteration this loop

will iterate once

for each object in the list and for each

iteration the variable next will be

bound to the current element in the list

or the next element in the list so the

first iteration will be bound to this

update 1 the second iteration to object

to store the string these strings and

then in the last iteration to a big

three and then I can for instance just

print out next here so and that is all I

had planned to say about the list for

this time there are more things to to

learn about the list interface and the

list implementations but once you start

working with them you will learn all the

little details there there are two to

learn about them and you know enough now

to get started using them so let's go

out there and get some practice with the

list

you