| Since Variables stand for items that the program is manipulating or playing with, such as: whole numbers (small normal large) fractional numbers (small large) single characters groups of characters as a unit true / false conditions it is only logical to assume that JAVA gives the programmer the capability to declare and use Variables that can hold each of the above data types. Remember that values ( or data ) being put into Variables is the heart or basis of programming. Variables MUST be declared before they can be used in a program. Common Variable types are as follows: short ... small integers int ... normal integers long ... big integers float ... small fractional numbers double ... big fractional numbers char ... single character values e.g., M or F string ... groups of characters viewed as a unit Boolean ... logical TRUE or FALSE values In object oriented and more sophisticated programs today, the programmer can declare his or her own data type ( later ) Specifics concerning Java Data TYpes NUMERIC (all numbers are signed) short integer (16 bit number) --> i.e. 32,768 int integer (32 bit number) --> i.e. 2 billion or 2 x 109 long integer (64 bit number) --> i.e. 9 x 1018 float real (32 bit decimal) --> i.e. 1.4 x 10-45 to 3.4 x 1038 double real (64 bit decimal) --> i.e. 4.9 x 10-324 to 1.7 x 10308 CHARACTER / STRING char a unicode (2-byte) char the String class handles multi-character strings (strings are unicode, as well). Strings are said to be immutable: updates to a String cause the new string to be created in memory (and pointed to by the String class). However the old string is not actually "updated", rather, its reference is lost (and it becomes available for garbage collection). BOOLEAN boolean (with values true, false) DATE the Date class measures the milliseconds since Jan 1, 1970 and provides supporting functions to interpret and manipulate date/times ARRAYS arrays hold multiple instances of a data type or object |
Sunday, September 12, 2010
Variable Types
Variable Types
Sorting an Array
Hello programmers.. my students.. i want to create a program in java that sort a number in descending order.
here is a sample program...
public class Sorting
{
public static void main(String args[])
{
int arr[]={0,1,2,3,4,5,6,7,8,9,10};
int temp=0;
for(int i=0; i<10;i++)
{
for(int j=0; j<10; j++)
{
if(arr[j]
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int x=0; x<10; x++)
{
System.out.println(arr[x]+ " ");
}
}
}
i hope you learn more in my blog,.... Think it Code it
here is a sample program...
public class Sorting
{
public static void main(String args[])
{
int arr[]={0,1,2,3,4,5,6,7,8,9,10};
int temp=0;
for(int i=0; i<10;i++)
{
for(int j=0; j<10; j++)
{
if(arr[j]
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int x=0; x<10; x++)
{
System.out.println(arr[x]+ " ");
}
}
}
i hope you learn more in my blog,.... Think it Code it
Programming Tuturials
hello programmers.... hello students... java programmers.. visit this website to study advannce.
http://webpages.shepherd.edu/JROMANO/arrays.htm
http://webpages.shepherd.edu/JROMANO/arrays.htm
Labels:
java tutorial,
programming languages
Array Definition and samples
An array is multiple storage locations under one name. For now
we will talk about one and two dimensional arrays.
All the primitive data types that we have learned and used concerning
regular variables also apply to arrays.
int num = new int; // declare / initialize an integer variable
int nums[] = new int [10]; // declare and allocate space for 10 integer
// numbers. The 10 elements have the same
// name but each one will be referred to
// with a different subscript or position
// in the array. Thos array contains 10
// integer locations, number 0 thru 9
Anything that you can do with an integer variable, you can do with
each element of an integer array. But you must indicate which element
you are referring to.
e.g.. nums[0]=100;
nums[5]=200; etc.
JOptionPane.showMessageDialog(null,"answer is "+nums[2]);
Loops make working with arrays very easy.
for example:
int tot=100;
int n=0;
do
{
nums[n] = tot;
n=n+1;
} while (n <= 9 );
Remember that all rules that apply to variables of a specific type
also apply to each element of an array of that type.
Two dimensional arrays adhere to all the rules noted above, except
any reference to a two dimensional array must have 2 subscripts.
int numbers[] [] = new int [5] [10];
The above array has 5 rows and 10 columns.
The rows are numbered 0 thru 4, and the columns are numbered 0 thru 9.
Any reference to the array will have 2 subscripts and the first one will be
the row and the second one will be the column.
e.g.. numbers[1][3] = 100;
Loops are very useful with two dimensional arrays just like one
dimensional arrays, but a lot of the times nested loops are used
because of the nature of two dimensional arrays.
e.g.. int tot=10;
for(int a=0;a<=4;a++)
{
for(int b=0;b<=9;b++)
{
numbers[a][b] = tot;
tot=tot+10;
}
}
.
arrays can be declared but still need to be explicitly initialized
int [] dataPoints; // declares the array of integer
dataPoints = new int[10] ; // allocates array size (now usable)
dataPoints = new int[numPoints]; // allocates array size with size specified by a run-time variable
an array can be re-initialized (with the same data type) at runtime, at which point old array entries are discarded
Java standard arrays cannot are not dynamically resizable like arrays in Visual Basic or PowerBuilder (i.e. where the language can dynamically extend the array, if needed). Java throws an exception if an out-of-bounds array index is referenced. Java.util's Vector class provides dynamic array-like capability (a Vector can store a collection of any type of object). The standard Vector class does not, however, limit data to a specific data type (data type limitation capability has to be built in a Vector extension).
we will talk about one and two dimensional arrays.
All the primitive data types that we have learned and used concerning
regular variables also apply to arrays.
int num = new int; // declare / initialize an integer variable
int nums[] = new int [10]; // declare and allocate space for 10 integer
// numbers. The 10 elements have the same
// name but each one will be referred to
// with a different subscript or position
// in the array. Thos array contains 10
// integer locations, number 0 thru 9
Anything that you can do with an integer variable, you can do with
each element of an integer array. But you must indicate which element
you are referring to.
e.g.. nums[0]=100;
nums[5]=200; etc.
JOptionPane.showMessageDialog(null,"answer is "+nums[2]);
Loops make working with arrays very easy.
for example:
int tot=100;
int n=0;
do
{
nums[n] = tot;
n=n+1;
} while (n <= 9 );
Remember that all rules that apply to variables of a specific type
also apply to each element of an array of that type.
Two dimensional arrays adhere to all the rules noted above, except
any reference to a two dimensional array must have 2 subscripts.
int numbers[] [] = new int [5] [10];
The above array has 5 rows and 10 columns.
The rows are numbered 0 thru 4, and the columns are numbered 0 thru 9.
Any reference to the array will have 2 subscripts and the first one will be
the row and the second one will be the column.
e.g.. numbers[1][3] = 100;
Loops are very useful with two dimensional arrays just like one
dimensional arrays, but a lot of the times nested loops are used
because of the nature of two dimensional arrays.
e.g.. int tot=10;
for(int a=0;a<=4;a++)
{
for(int b=0;b<=9;b++)
{
numbers[a][b] = tot;
tot=tot+10;
}
}
.
arrays can be declared but still need to be explicitly initialized
int [] dataPoints; // declares the array of integer
dataPoints = new int[10] ; // allocates array size (now usable)
dataPoints = new int[numPoints]; // allocates array size with size specified by a run-time variable
an array can be re-initialized (with the same data type) at runtime, at which point old array entries are discarded
Java standard arrays cannot are not dynamically resizable like arrays in Visual Basic or PowerBuilder (i.e. where the language can dynamically extend the array, if needed). Java throws an exception if an out-of-bounds array index is referenced. Java.util's Vector class provides dynamic array-like capability (a Vector can store a collection of any type of object). The standard Vector class does not, however, limit data to a specific data type (data type limitation capability has to be built in a Vector extension).
Labels:
array,
array definition,
java array
Wednesday, September 8, 2010
Array sample program
hello world.. hello programmers... i want to share to you how to make a sample program array using java..
I hope you learn and enjoy reading my blog...
import java.io.*;
public class javaarray
{
public static void main(String args[])
{
int x[]=new int[3];
x[0]=100;
x[1]=200;
x[2]=300;
//display result
System.out.println("The array[0]=" + x[0]);
System.out.println("The array[1]=" + x[1]);
System.out.println("The array[2]=" + x[2]);
}
}
next time i will post another sample program using array. using loop to print the array element.
I hope you learn and enjoy reading my blog...
import java.io.*;
public class javaarray
{
public static void main(String args[])
{
int x[]=new int[3];
x[0]=100;
x[1]=200;
x[2]=300;
//display result
System.out.println("The array[0]=" + x[0]);
System.out.println("The array[1]=" + x[1]);
System.out.println("The array[2]=" + x[2]);
}
}
next time i will post another sample program using array. using loop to print the array element.
Array in java
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.
Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array
An array of ten elements
Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.
class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declares an array of integers
anArray = new int[10]; // allocates memory for 10 integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
}
}
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
In a real-world programming situation, you'd probably use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as shown above. However, this example clearly illustrates the array syntax. You'll learn about the various looping constructs (for, while, and do-while) in the Control Flow section.
Declaring a Variable to Refer to an Array
The above program declares anArray with the following line of code:
int[] anArray; // declares an array of integers
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Creating, Initializing, and Accessing an Array
One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to the anArray variable.
anArray = new int[10]; // create an array of integers
If this statement were missing, the compiler would print an error like the following, and compilation would fail:
ArrayDemo.java:4: Variable anArray may not have been initialized.
The next few lines assign values to each element of the array:
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
Here the length of the array is determined by the number of values provided between { and }.
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.
In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:
class MultiDimArrayDemo {
public static void main(String[] args) {
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}};
System.out.println(names[0][0] + names[1][0]); //Mr. Smith
System.out.println(names[0][2] + names[1][1]); //Ms. Jones
}
}
The output from this program is:
Mr. Smith
Ms. Jones
Finally, you can use the built-in length property to determine the size of any array. The code
System.out.println(anArray.length);
will print the array's size to standard output.
Copying Arrays
The System class has an arraycopy method that you can use to efficiently copy data from one array into another:
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.
The following program, ArrayCopyDemo, declares an array of char elements, spelling the word "decaffeinated". It uses arraycopy to copy a subsequence of array components into a second array:
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
The output from this program is:
caffein
next time i will post another exam program using java.
Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array
An array of ten elements
Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.
class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declares an array of integers
anArray = new int[10]; // allocates memory for 10 integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
}
}
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
In a real-world programming situation, you'd probably use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as shown above. However, this example clearly illustrates the array syntax. You'll learn about the various looping constructs (for, while, and do-while) in the Control Flow section.
Declaring a Variable to Refer to an Array
The above program declares anArray with the following line of code:
int[] anArray; // declares an array of integers
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Creating, Initializing, and Accessing an Array
One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to the anArray variable.
anArray = new int[10]; // create an array of integers
If this statement were missing, the compiler would print an error like the following, and compilation would fail:
ArrayDemo.java:4: Variable anArray may not have been initialized.
The next few lines assign values to each element of the array:
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
Here the length of the array is determined by the number of values provided between { and }.
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.
In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:
class MultiDimArrayDemo {
public static void main(String[] args) {
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}};
System.out.println(names[0][0] + names[1][0]); //Mr. Smith
System.out.println(names[0][2] + names[1][1]); //Ms. Jones
}
}
The output from this program is:
Mr. Smith
Ms. Jones
Finally, you can use the built-in length property to determine the size of any array. The code
System.out.println(anArray.length);
will print the array's size to standard output.
Copying Arrays
The System class has an arraycopy method that you can use to efficiently copy data from one array into another:
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.
The following program, ArrayCopyDemo, declares an array of char elements, spelling the word "decaffeinated". It uses arraycopy to copy a subsequence of array components into a second array:
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
The output from this program is:
caffein
next time i will post another exam program using java.
Subscribe to:
Posts (Atom)