JAVA Beginners part 8

JAVA API , these built in classes are already compiled. That means you dont have to type them.Just use them.

Sample API to start – ArrayList

Arraylist is a advanced array. that can hold items just an array with additional features, which i will showcase below.

Declare this way

ArrayList<egg> Mylist = new ArrayList<egg>(); // An Arraylist of Objects. Note the angular brackets

egg s = new egg();
egg b = new egg();

Mylist.add(s);
Mylist.add(b);

Features of Array List

int size           = Mylist.size();            // return the number of objects in the array list
boolean isin = Mylist.contains(s); // checks if the object s is in the array list
int index       = Mylist.indexof(s);    // returns the index. Note the index starts from 0, so the index of s will be 0
boolean empty = Mylist.isEmpty(); // return false in this case
Mylist.remove(s);                                // removes s from the list

other Features

remove object from list. Can allocate size dynamically( meaning you can expand the size to any limit). Arraylist<type> type says of the type is 

change.jpg

What is the benefit of using array list?

Using Array list the code becomes easy and more scale able. With Array List if you enter the same input again it will not count as a hit, one may not follow this completely without the full background from the book. so i suggest to go through that to get better idea.   

The Final Code of the game really sucks. Over explanation of same thing again and again is irritating and boring than compelling. So, I wont go through that. Dealing with array.

Euler Problem 1 – Sum of all multiples of 3 or 5 less than 100 using Array List.

euler1.jpg

If you really really want to learn programming, then programming has to be made for fun or you could make it interesting by yourself. I have included few sites below that gives mathematical or a real life situation that you need to solve using a programming language. Since we are learning JAVA why not use the same.

I am trying few problems for fun. You may try based on your likes/dislikes.

Interview Cake Problem One

J2

Note – There is no gap between >= in Java.

Not equals Boolean Expression != or !

y3

Java.Lang package holds many classes like String, Math or System

Why do we need packages?

To group the classes. Classes can be grouped based on functionalities
It gives naming scope to prevent name clashes . example various people can use the same name for a method,provided they do it in their own package.
Provides security.

Interview tip !! A full class name would be with extension from package and class name.

Two ways to import a class

1 use import as first statement. Like import.java.util.ArrayList
or
2 use inline where ever you want to use, java.util<ArrayList>Dog list = new.

Java.Lang package does not need to be imported, it is done automatically ( They are fundamental). So the classes within that package can be used directly.

Ponder. Import doesn’t add overhead to your code. Lets say you have 10 import statement , it is not like there would be additional class loading etc.
Import is a way to tell JVM full name of class.

Few Interesting Questions from Sample Question List of Oracle Certified Java Certification 

test1

test2.jpg

test3

 

next page 181

More in next part.

References:

Head First Java 2nd Edition

https://projecteuler.net

/https://www.interviewcake.com/

http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=303&p_certName=SQ1Z0_803

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s