Java Programs
Java Programs
class Practice {
*
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
/*
//define limit
int limit = 100;
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;
/*
To copy all elements of java ArrayList object into array use
Object[] toArray() method.
*/
/*
Output would be
ArrayList elements are copied into an Array. Now Array Contains..
1
2
3
4
5
*/