الأربعاء، 6 ديسمبر 2017

Java ArrayList remove Example: How to remove by index, by Value/Object, for a specific range


How to remove a value from ArrayList in java with example programs.

In this post, we will learn how to program to remove elements from a ArrayList in java. Removing value can be done in three ways.

1) By index
2) By value or Object
3) For a given specific range

ArrayList api provides various methods to do remove operations.

java-arraylist-remove




الأحد، 19 نوفمبر 2017

Encapsulation in Java with Examples

What is Encapsulation in Java 

Encapsulation is one of the principle of OOPs.

Encapsulation describes the ability of hiding data and methods of object.
Encapsulation is a process of arranging information about data and behavior into a single component like as object in Java.

Keeping all members and methods are within bounds of class. In Java, all class are under encapsulated.

If we set all fields of class as private, so no other code outside of this class can not access private variables. To access these private variables, we have to declare public methods in class.


So these private members are not accessible by outside objects or classes.

A class can have authority to modify the values using setter and getter methods of class. By using getter methods, we can make read-only and wise versa for write-only.

الأربعاء، 15 نوفمبر 2017

3 Ways to Find Sum of First n numbers program in Java

In this post, You will learn today today how to find sum of first n numbers in java using while loop, for loop and mathematics formula(Optimized).

For this program, we have to input the natural number n value which you want to get the sum. So that Our program will get sum of first n numbers. This is basic program for freshers or who are studying engineering in their lab programs.



Example 1:
number = 10
sum = 55

Example 2:
number = 100
sum = 5050

الجمعة، 10 نوفمبر 2017

Java Supports Pass By Value only, not Pass By Ref

Does Java supports Pass By Value or Pass By Reference:

 

Very common question in java interviews for freshers and experienced. But, this is very tricky and confusion for most of the people whether java is pass by value or pass by reference.

First, we will make clear what is call by value and call by reference. These two comes into play when we pass parameters in invoking a method.

java-pass-by-value




Call By Value: Values only passed as copy of original. But not original memory location. It creates a new copy in the memory and passes to the method that we invoked.

Call By Reference: Passes reference or alias of the object which is said to be Call By Reference. Unfortunately, Java inventors decided to call the location of an object as a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.

الخميس، 9 نوفمبر 2017

Iterating String Array Examples in Java - Iterate Through String Array in Java

For-Each Example: Enhanced for Loop to Iterate Java Array:

How to iterate String array in Java. This is a common scenario which is being used in our projects.
Let us take a string array example that we want to iterate over it.

Note: Array index always starts from 0 programmatically.


String Array Iterating:

A String Array is a data structure that holds several String values. The number of String elements is fixed and can't be changed. Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. 

We can do this in mainly three ways.

Step 1): Using for loop
Step 2): Using while loop
Step 3): Using For-Each loop


Iterating String Array Examples in Java


السبت، 4 نوفمبر 2017

Java Program to Add Three Numbers (With Possible Runtime Exception)

1. Overview:


In this post, we will learn how to add three numbers in java with simple example program. We will explain step by step explanation.

In mathematics, the summation is calculated by using '+' [plus] operator. We will achieve the sum of three numbers using '+' operator in java.

Sum Of Three Numbers in Java

Formula:


sum = a + b + c;

الاثنين، 30 أكتوبر 2017

Java Program to Bubble Sort(Sinking Sort)

Bubble Sort in Java | Sinking Sort Technique

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

1) Easy to understand.
2) Easy to implement.
3) In-place, no external memory is needed.
4) Performs greatly when the array is almost sorted (This is part of Optimized Bubble Sort).
5) Widely used by students to clear practical exams or freshers interview process as the end result is the same as that of any sorting technique


Bubble Sort in Java

الأحد، 29 أكتوبر 2017

4 Best Ways To Swap Two Numbers in Java without using temporary/Third variable?

In this tutorial, We will discuss how to swap two numbers in Java without using temporary variable.


This is very common interview question for freshers or 1-2 experience.

First, we will see how to swap using two numbers with using temporary variable then next without third variable.

Sum of  Two Numbers in Java

4 Best Ways To Swap Two Numbers in Java without using temporary variable?

الخميس، 26 أكتوبر 2017

4 Different ways of Reading a text file in Java (CSV Files)

In this post, We will learn very basic program How to read a file in Java. Very useful scenario which is prominently used in our real time projects.
How to read csv file in java. This is a common use case.

java.io package has utility classes for reading and writing files.

We can do reading a file in java 4 ways.

1) Using BufferedReader
2) Using JDK 7 try-with-resources
3) Using Files.readAllBytes
4) Using Files.readAllLines

4 Ways How to read a file in Java, CSV Example Programs

First, Create a csv file with the following data in your computer(File name : employee.csv).

100,Jhon,20
101,Cena,24
102,Mike,19

الأربعاء، 18 أكتوبر 2017

Java Type Inference in Generics

Type Inference in Generic

In Java 7, Type Inference is introduced in generics declaration which is used to reduce the coding as well to minimize the compile errors.

Made compiler smart enough to infer the type of generic instance. This is done by using "<>" operator and it is called as Diamond Operator. Not required to mention the type in between diamond operator and can be replaced declaring the type as left blank.

Java Type Inference in Generics

الاثنين، 16 أكتوبر 2017

How to convert a String to an int in Java?

We will learn today, How to convert String to Int in java. This is very popular written test question in Java interviews.

For example, We have a String "12345" and now I want to represent it in integer.

We can do conversion in 4 ways.

1) Integer.parseInt method.
2) Integer.valueOf
3) Integer constructor
4) DecimalFormat class

string to int

Add ArrayList to another ArrayList in Java | addAll method

In this post, We will learn How to add ArrayList into a another ArrayList in java.

We can accumulate this in two ways.

1) While creating ArrayList object.
2) Using addAll method.


Add ArrayList to another ArrayList in Java


1) Adding existing ArrayList into new list:

ArrayList has a constructor which takes list as input. Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(Collection c)

الخميس، 21 سبتمبر 2017

Java API ArrayList Example

Java API ArrayList Overview

In this tutorial, We'll learn about Java Collection API ArrayList.
ArrayList is a class which is widely used in real time applications. It is in package java.util and public class. This is added in java 1.2 version.

Java API ArrayList Example


Class Name: ArrayList
First Version: 1.2
Super Class: AbstractList
Implemented interfaces: List, RandomAccess, Cloneable, Serializable
Pacakge: java.util.

السبت، 9 سبتمبر 2017

Java Enhanced For loop, Examples

Enhanced for loop in java, Java enhanced for loop, Java for-each loop, Enhanced for loop example, Advantageous of for-each loop. Iterating through basic for loop. Iterating through Arrays, Iterating through Collection

Enhanced for loop to overcome the drawbacks of basic for loop and this best suitable for iterating Arrays and Collections.

Syntax:


for ( varaible_declaration : Array or Collection)
{
    //
}

Java Enhanced For loop, Examples

Java Continue Statement, Label loop

Java Continue Statement, Continue Statement in Java, Continue Examples in java, Java continue in inner loop, inner loop continue example, Continue Statement Examples.

Continue statement is used to skip the current loop iteration when a condition is satisfied.
Mainly used in while or for loop.

Syntax:

continue;

Example Flow chart:

Java Continue Statement, Label loop

الثلاثاء، 5 سبتمبر 2017

الأحد، 27 أغسطس 2017

Introduction to Java Collection over Arrays

Arrays Vs Collections

 An array is an indexed collection of fixed number of homogeneous data elements.
 The main advantageous of array is we can represent multiple values by using single variable. So
 that readability of the code will be improved.

29. Collection over Arrays