ARRAYS IN JAVA




Hello Friends Now I am going to learn that how we use Arrays 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:

Example:

double[] myList;   // preferred way.

or

double myList[];         //  works but not preferred way.
 

Creating Arrays:

You can create an array by using the new operator with the following syntax:

arrayRefVar = new dataType[arraySize];
 
 
The above statement does two things:
  • It creates an array using new dataType[arraySize];
  • It assigns the reference of the newly created array to the variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below:

     dataType[] arrayRefVar = new dataType[arraySize];
 
Alternatively you can create arrays as follows: 
 
dataType[] arrayRefVar = {value0, value1, ..., valuek};  
 
The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1.  
 

Example:

Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList:

double[] myList = new double[10];
 
Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9.
 

 

Processing Arrays:

When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known.

Example:

Here is a complete example of showing how to create, initialize and process arrays:
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); } // Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total); // Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); } } This would produce the following result:
1.9 2.9 3.4 3.5 Total is 11.7 Max is 3.5

The foreach Loops:

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.

Example:

The following code displays all the elements in the array myList:
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } } } This would produce the following result:
1.9 2.9 3.4 3.5

Passing Arrays to Methods:

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:
 
public static void printArray(int[] array)  
{
  for (int i = 0; i < array.length; i++)
  {
  System.out.print(array[i] + " ");
  }
  }
 
You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2:
printArray(new int[]{3, 1, 2, 6, 4, 2});

Returning an Array from a Method:

A method may also return an array. For example, the method shown below returns an array that is the reversal of another array:

public static int[] reverse(int[] list)
  {
  int[] result = new int[list.length];
  for (int i = 0, j = result.length - 1; i < list.length; i++, j--)
  {
  result[j] = list[i];
  }
  return result;
  }

 
I hope this post will help you.
Thanks !!
If still you have some query regarding to this then please post your comment after that i will 100% help you…:)

 

 

 



FIND YOUR LOVE



To find love, you must first be willing to look for it

FINDING YOUR LOVE



Love is not finding someone to live with, It's finding someone
.
.
.
you can't live without you..


TEMPLATES IN C++



Templates are the foundation of generic programming which involves writing code in a way that is independent of any particular type.

Hello Friends Today I am going to learn about Template in C++. That how to use Template inn C++. A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators, and algorithms are examples of generic programming and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector or vector .
You can use templates to define functions as well as classes, let us see how do they work:
Function Template: The general form of a template function definition is shown here:
template ret-type func-name(parameter list)
{
// body of function
}
Here, type is a placeholder name for a data type used by the function. This name can be used within the function definition.
The following is the example of a function template that returns the maximum of two values:
#include
#include
using namespace std;
template
inline T const& Max (T const& a, T const& b)
{
return a < b ? b:a;
}
int main ()
{
int i = 39;
int j = 20;
cout << "Max(i, j): " << Max(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;
string s1 = "Hello";
string s2 = "World";
cout << "Max(s1, s2): " << Max(s1, s2) << endl;
return 0;
}

If we compile and run above code, this would produce following result:
Max(i, j): 39
Max(f1, f2): 20.7
Max(s1, s2): World

Class Template:
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:
template class class-name {
.
.
.
}
Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more than one generic data type by using a comma-separated list.
Following is the example to define class Stack<> and implement generic methods to push and pop the elements from the stack:
#include
#include
#include
#include
#include
using namespace std;
template
class Stack {
private:
vector elems; // elements
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const{ // return true if empty.
return elems.empty();
}
};
template
void Stack::push (T const& elem)
{
// append copy of passed element
elems.push_back(elem);
}
template
void Stack::pop ()
{
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// remove last element
elems.pop_back();
}
template
T Stack::top () const
{
if (elems.empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
// return copy of last element
return elems.back();
}
int main()
{
try {
Stack intStack; // stack of ints
Stack stringStack; // stack of strings
// manipulate int stack
intStack.push(7);
cout << intStack.top() < // manipulate string stack
stringStack.push("hello");
cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
}
catch (exception const& ex) {
cerr << "Exception: " << ex.what() < return -1;
}
}
If we compile and run above code, this would produce following result:
7
hello
Exception: Stack<>::pop(): empty stack
I think this post will help you.I think this post will help you.
Thanks!!!
If any query about this post then put your comment in comment box below..

PROGRAM OF CONSTRUCTOR OVERLOADING





Hello Friends!!!!!! Today I am Going to learn about the constructor oveloading. Overloading of a constructor means that we use more than one constructor in a single program.A program is given below which shows you concept of constructor overloading......

So Let's Start=>

//Name : Mayank Sharma //
//Roll No : 1101026 //
//CSE(4th Sem)

PROGRAM OF CONSTRUCTOR OVERLOADING

#include
#include
class distance
{
int feet;
float inches;
public:
distance()
{
feet=0;
inches=0;
}
distance(int ft, float in)
{
feet=ft;
inches=in;
}
void get_dist()
{
cout<<"Enter feet :";
cin>>feet;
cout<<"Enter Inches :";
cin>>inches;
}
void show_dist()
{
cout< }
void add_dist(distance,distance);
};
void distance :: add_dist(distance d2,distance d3)
{
inches=d2.inches+d3.inches;
feet=0;
if(inches>=12.0)
{
inches-=12.0;
feet++;
}
feet+=d2.feet+d3.feet;
}
void main()
{
clrscr();
distance d1,d3;
distance d2(11,7.56);
d1.get_dist();
d3.add_dist(d1,d2);
cout<<"\n d1=";
d1.show_dist();
cout<<"\n d2=";
d2.show_dist();
cout<<"\n d3=";
d3.show_dist();
getch();
}

I think this post will help you.I think this post will help you.
Thanks!!!
If any query about this post then put your comment in comment box below..

PROGRAM TO ILLUSTRATE THE CONCEPT OF OPREATOR OVELOADING





Hello Friends!!!! Today I am going to learn about the concept of operator overloading using a simple vector program. In this program we have a class vector and a multiply operator to use and oveload.

So Let's Start=>

//Name : Mayank Sharma //
//Roll No : 1101026 //
//CSE(4th Sem)

PROGRAM TO ILLUSTRATE THE CONCEPT OF OPREATOR OVELOADING

#include
#include
const size=3;
class vector
{
int v[size];
public:
vector();
vector(int *x);
friend vector operator * (int a, vector b);
friend vector operator * (vector b, int a);
friend istream & operator >>(istream &,vector &);
friend ostream & operator <<(ostream &, vector &);
};
vector :: vector()
{
for(int i=0;i v[i]=0;
}
vector :: vector(int *x)
{
for(int i=0;i v[i]=x[i];
}
vector operator * (int a,vector b)
{
vector c;
for(int i=0;i c.v[i]=a*b.v[i];
return c;
}
vector operator *(vector b, int a)
{
vector c;
for(int i=0;i c.v[i]=b.v[i]*a;
return c;
}
istream & operator >> (istream &din, vector &b)
{
for(int i=0;i din>>b.v[i];
return(din);
}
ostream & operator << (ostream &dout, vector &b)
{
for(int i=0;i dout< return(dout);
}
int x[size]={2,4,6};
void main()
{
clrscr();
vector m;
vector n=x;
cout<<"Enter Elements of vector m"<<"\n";
cin>>m;
cout<<"\n";
cout<<"m="< vector p,q;
p=2*m;
q=n*2;
cout<<"\n";
cout<<"p="< cout<<"q="< getch();
}

I think this post will help you.I think this post will help you.
Thanks!!!
If any query about this post then put your comment in comment box below..

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites