Java short Array

It is used to store short data type values only. With the following examples you can learn how to declare short array, how to assign values to short array and how to get values from short array.

Java short Array Example
/* short Array Example */
/* Save with file name ShortArray.java */

public class ShortArray
{
	public static void main(String args[])
	{
		//SHORT ARRAY DECLARATION
		short s[];
		//MEMORY ALLOCATION FOR SHORT ARRAY
		s = new short[4];
		//ASSIGNING ELEMENTS TO SHORT ARRAY
		s[0] = 10;
		s[1] = 30;
		s[2] = 40;
		s[3] = 7;
		//SHORT ARRAY OUTPUT
		System.out.println();
		System.out.println("short Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
	}
}
                        

In this example you can learn how to assign values to short array at the time of declaration.

Java short Array Example 2
/* short Array Example 2 */
/* Save with file name ShortArray2.java */

public class ShortArray2
{
	public static void main(String args[])
	{
		//SHORT ARRAY DECLARATION AND ASSIGNMENT
		short s[] = {10,30,40,7};
		//SHORT ARRAY OUTPUT
		System.out.println();
		System.out.println("short Array Example");
		System.out.println("==================");
		System.out.println();
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
	}
}
                        

In this example you can learn how to declare short array with other short variables.

Java short Array Example 3
/* short Array Example 3 */
/* Save with file name ShortArray3.java */

public class ShortArray3
{
	public static void main(String args[])
	{
		//SHORT ARRAY DECLARATION
		short s[], s2; //s IS AN ARRAY s2 IS NOT AN ARRAY
		//MEMORY ALLOCATION FOR SHORT ARRAY
		s = new short[4];
		//ASSIGNING ELEMENTS TO SHORT ARRAY
		s[0] = 10;
		s[1] = 30;
		s[2] = 40;
		s[3] = 7;
		s2 = 101;
		//SHORT ARRAY OUTPUT
		System.out.println();
		System.out.println("short Array Example");
		System.out.println("==================");
		System.out.println();
		System.out.println("value of s2 : "+s2);
		System.out.println();
		for(int i=0;i<s.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s[i]);
		}
	}
}
                        

In this example you can learn how to assign short array to other short array.

Java short Array Example 4
/* short Array Example 4 */
/* Save with file name ShortArray4.java */

public class ShortArray4
{
	public static void main(String args[])
	{
		//SHORT ARRAY DECLARATION
		short[] s, s2; //s AND s2 ARE ARRAY VARIABLES
		//MEMORY ALLOCATION FOR SHORT ARRAY
		s = new short[4];
		//ASSIGNING ELEMENTS TO SHORT ARRAY
		s[0] = 10;
		s[1] = 30;
		s[2] = 40;
		s[3] = 7;
		//ASSIGNING s ARRAY TO s2 ARRAY VARIABLE
		s2 = s;
		//SHORT ARRAY OUTPUT
		System.out.println();
		System.out.println("short Array Example");
		System.out.println("==================");
		System.out.println();
		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();
		System.out.println("s2 array values");
		for(int i=0;i<s2.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + s2[i]);
		}
	}
}