Skip to main content

Posts

Showing posts from February, 2016

Linkedlist in Java

LinkedList in Java LinkedList  is an implementation of  List   interface. Example of LinkedList in Java import java . util .*; public class LinkedListExample { public static void main ( String args []) { /* Linked List Declaration */ LinkedList < String > linkedlist = new LinkedList < String >(); /*add(String Element) is used for adding * the elements to the linked list*/ linkedlist . add ( "Item1" ); linkedlist . add ( "Item5" ); linkedlist . add ( "Item3" ); linkedlist . add ( "Item6" ); linkedlist . add ( "Item2" ); /*Display Linked List Content*/ System . out . println ( "Linked List Content: " + linkedlist ); /*Add First and Last Element*/ linkedlist . addFirst ( "First Item" ); linkedlist . addLast ( "Last Item" );

Arrays in Java

Array in Java Java provides a data structure, the  array , which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. Declaring Array Variables: To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable: dataType [] arrayRefVar ; // preferred way. int[] numbers; or dataType arrayRefVar []; // works but not preferred way. int num