Java short Array - short Array in Java
short Array in Java
Java short Array
Java short array is used to store short data type values only. The default value of the elements in a short array is 0.
With the following Java short array examples you can learn
- how to declare Java short array
- how to assign values to Java short array
- how to get values from Java short array
Initializing a short Array in Java
Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.
If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.
// declares an array of shorts
short[] shortArray;
What is the Length of an Array in Java ?
In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.
Syntax :
< data type > < variable >[];
< data type >[] < variable >;
Java short Array Example
Following example shows Java short Array . Save the following Java short Array example program with file name JavaShortArrayExample.java .
public class JavaShortArrayExample { public static void main(String args[]) { // JAVA SHORT ARRAY DECLARATION short s[]; // MEMORY ALLOCATION FOR JAVA SHORT ARRAY s = new short[4]; //ASSIGNING ELEMENTS TO JAVA SHORT ARRAY s[0] = 10; s[1] = 30; s[2] = 40; s[3] = 7; // JAVA SHORT ARRAY OUTPUT System.out.println("Java short Array Example"); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } } }
Related : Java boolean Array
How to assign values to Java short array at the time of Java short array declaration ?
Following Java short array example you can learn how to assign values to Java short array at the time of Java short array declaration.
Assign values to Java short array at the time of Java short array declaration Example
/* How to assign values to Java short array at the time of Java short array declaration Example Save with file name ShortArray2.java */ public class ShortArray2 { public static void main(String args[]) { //JAVA SHORT ARRAY DECLARATION AND ASSIGNMENT short s[] = {10,30,40,7}; //JAVA SHORT ARRAY OUTPUT System.out.println("Java short Array Example"); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } } }
Related : Java byte Array
How to declare Java short array with other Java short variables ?
Following Java short array example you can learn how to declare Java short array with other Java short variables.
Following example shows How to declare Java short array with other Java short variables . Save the following declare Java short array with other Java short variables Example program with file name DeclareShortArrayWithShortVariables.java .
Declare Java short array with other Java short variables Example
public class DeclareShortArrayWithShortVariables { public static void main(String args[]) { short s[], s2; s = new short[4]; s[0] = 10; s[1] = 30; s[2] = 40; s[3] = 7; s2 = 101; System.out.println("Java short Array Example"); System.out.println("value of s2 : "+s2); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } } }
Related : Java char Array
How to assign Java short array to other Java short array ?
Following Java short array example you can learn how to assign Java short array to other Java short array.
Following example shows How to assign Java short array to other Java short array . Save the following assign Java short array to other Java short array example program with file name AssignShortArrayToShortArray.java .
Assign Java short array to other Java short array Example
public class AssignShortArrayToShortArray { public static void main(String args[]) { short[] s, s2; s = new short[4]; s[0] = 10; s[1] = 30; s[2] = 40; s[3] = 7; s2 = s; System.out.println("Java short Array Example"); System.out.println("s array values"); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } System.out.println("s2 array values"); for(int i=0;i<s2.length;i++) { System.out.println("Element at Index : "+ i + " " + s2[i]); } } }
Related : Java int Array
How to Sort short Array in Java ?
To sort Java short array, you can use the java.util.Arrays.sort(short[] short_array) method sorts the specified array of shorts into ascending order. Following example shows how to sort a Java short array using sort method of java.util.Arrays.
Sort short Array in Java Example
/* How to Sort short Array in Java Example Save with file name SortShortArray.java */ import java.util.*; public class SortShortArray { public static void main(String args[]) { short[] short_array = new short[] { 99, 33, 11, 44, 66, 77, 88, 22, 55 }; System.out.println("Before sorting Java short array"); for (short short_no : short_array) { System.out.println(short_no); } System.out.println("After Java short array sort"); Arrays.sort(short_array); for (short short_no : short_array) { System.out.println(short_no); } } }
Related : Java float Array