/*
Java String Array Length Example
This Java String Array Length example shows how to find number of elements
contained in an Array.
*/
public class JavaStringArrayLengthExample {
public static void main(String args[]){
//create String array
String[] strArray = new String[]{"Java", "String", "Array", "Length"};
/*
* To get length of array, use length property of array.
*/
int length = strArray.length;
System.out.println("String array length is: " + length);
//print elements of an array
for(int i=0; i < length; i++){
System.out.println(strArray[i]);
}
}
}
Java Sum of Array values Example
class Practice {
public static void main(String [] args) {
int[] num = new int[] {10,20,30,40,50};
double avg;
int sum=0;
for(int i = 0; i<num.length;i++) {
sum = sum + num[i];
}
avg = sum/num.length;
System.out.println("Res is :" + avg);
}
}
*
Copy all elements of Java Vector to an Object Array Example
This Java Example shows how to copy all elements of Java Vector object to an
array using copyInTo method.
*/
import java.util.Vector;
public class CopyElementsOfVectorToArrayExample {
public static void main(String[] args) {
//create a Vector object
Vector v = new Vector();
//Add elements to Vector
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
//declare an array to hold elements of Vector
Object[] objArray = new Object[5];
/*
To copy all elements of java vector object into array use
void copyInTo(Ojbect[] obj) method. Here obj is an array into which
elements will get copied.
Please note that the array should be big enough to hold all elements of
java vector object. If not, ArrayIndexOutOfBoundException would be thrown.
*/
v.copyInto(objArray);
//display contents of Object array
System.out.println("Vector elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
}
}
/*
Output would be
Vector elements are copied into an Array. Now Array Contains..
1
2
3
4
5
Find Largest and Smallest Number in an Array Example
This Java Example shows how to find largest and smallest number in an
array.
*/
public class FindLargestSmallestNumber {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*
Output of this program would be
Largest Number is : 98
Smallest Number is : 23
/*
Prime Numbers Java Example
This Prime Numbers Java example shows how to generate prime numbers
between 1 and given number using for loop.
*/
public class GeneratePrimeNumbersExample {
public static void main(String[] args) {
//define limit
int limit = 100;
System.out.println("Prime numbers between 1 and " + limit);
//loop through the numbers one by one
for(int i=1; i < 100; i++){
boolean isPrime = true;
//check to see if the number is prime
for(int j=2; j < i ; j++){
if(i % j == 0){
isPrime = false;
break;
}
}
// print the number
if(isPrime)
System.out.print(i + " ");
}
}
}
/*
Output of Prime Numbers example would be
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/
/*
Copy all elements of Java ArrayList to an Object Array Example
This Java Example shows how to copy all elements of Java ArrayList object to an
array of Objects using toArray method.
*/
import java.util.ArrayList;
public class CopyElementsOfArrayListToArrayExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to ArrayList
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
/*
To copy all elements of java ArrayList object into array use
Object[] toArray() method.
*/
Object[] objArray = arrayList.toArray();
//display contents of Object array
System.out.println("ArrayList elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
}
}
/*
Output would be
ArrayList elements are copied into an Array. Now Array Contains..
1
2
3
4
5
*/