import java.util.Vector; //Needed for the Vector itself.
import java.util.Scanner; //Needed for console input.
class Main extends Thread { //The extension/inheritance is only for the 3.5 second waits I've placed, not required for Vectors.
public static void main(String[] args) throws InterruptedException { //The throw is only for the 3.5 second waits I've placed, not required for Vectors.
int i=0, index=-1;
Integer element;
Integer[] exampleArray = new Integer[25];
//Info.
System.out.println("**This shows you a bit about the Vector class as an alternative to arrays.**\n");
Thread.sleep(3500); //These just has the app wait for 3.5 seconds to let you catch up.
/* Declare the Vector.
* NOTE: Primitives cannot be used; use autoboxing/autounboxing & wrappers instead (like Integer, below)
*/
Vector<Integer> exampleVector = new Vector<Integer>();
//We haven't initialized the Vector yet, so it's empty. See for yourself:
if(exampleVector.isEmpty()) System.out.println("Yup, it's empty alright.\n");
Thread.sleep(3500);
//Now we'll add to the Vector. Let's get it done quickly by using a for loop to add 10 items:
System.out.print("Here are the elements we're adding: \n");
for(i=0; i<10; i++) {
exampleVector.addElement(i);
System.out.print(i + " ");
}
System.out.println("\n");
Thread.sleep(3500);
/* But wait! I want to add 100 to the 4th position (index 3). If this were an array, we'd be running into a few problems here,
* but since this is a Vector, it's as easy as: */
exampleVector.insertElementAt(100, 3);
//Now, check it out (also notice the for condition's operator ( .size() ):
for (i=0; i<exampleVector.size(); i++) {
System.out.print(exampleVector.elementAt(i) + " "); //Also notice how we get at elements: the elementAt(index) dot-operator.
}
System.out.println("\n");
Thread.sleep(3500);
//We can check if the Vector contains a specific element anywhere by using the .contains(Object) method:
System.out.println("Is the number 2 in the Vector?");
if(exampleVector.contains(2) == true) System.out.println("--> Yup.\n");
else System.out.println("--> Nope.\n");
Thread.sleep(3500);
//Ok. So now we know an element is present. What if I want to know its index so I can change it? It's this easy:
index = exampleVector.indexOf(2);
System.out.println("2 is at the index " + index + "\n");
Thread.sleep(3500);
/* Actually, I've decided that I don't want 2 in the list anymore. I want to delete it.
* Again, if this were an array, this would present some problems, but with a Vector, all we do is: */
exampleVector.removeElement(2);
//We also could have used the following code: exampleVector.removeElementAt(exampleVector.indexof(2)); -- they're equivalent.
//Let's see what happened:
for (i=0; i<exampleVector.size(); i++) {
System.out.print(exampleVector.elementAt(i) + " ");
}
System.out.println("\n");
Thread.sleep(3500);
//**The really cool thing about Vectors is that their actual size is dynamic. That element we removed
// actually shortened the whole Vector. Arrays are fixed in size once they are declared - inconvenient, huh?
//Of course, there are tons of other Vector methods; I suggest you head over to the API & check them out. Here are a couple:
//firstElement:
System.out.println("What's the first element in the Vector?");
element = exampleVector.firstElement();
System.out.println("--> " + element);
//lastElement:
element = exampleVector.lastElement();
System.out.println("What's the last element?\n--> " + element + "\n");
Thread.sleep(3500);
//If, for some weird reason, you really need an array, you can copy your Vector into an array like this:
exampleVector.copyInto(exampleArray);
//Let's see if it's really there:
for(i=0; i< 25; i++) { //25 is the length of the array above.
if(exampleArray[i] != null) System.out.print(exampleArray[i] + " ");
}
System.out.println("\n");
Thread.sleep(3500);
//Lastly, we can clear the Vector to end with what we started with like this:
exampleVector.removeAllElements();
System.out.println("Empty?");
if(exampleVector.isEmpty() == true) System.out.println("Yup, it is.\n");
else System.out.println("Nope. \n");
Thread.sleep(3500);
System.out.println("Well, that's the end of this tutorial/snippet. Hope you learned a bit about the advantages of using Vecotrs." +
"\nIf you're interested in learning more, you should check out the API to unlock all the Vector class' possibilities in your apps.!");
}
}
import java.util.Scanner; //Needed for console input.
class Main extends Thread { //The extension/inheritance is only for the 3.5 second waits I've placed, not required for Vectors.
public static void main(String[] args) throws InterruptedException { //The throw is only for the 3.5 second waits I've placed, not required for Vectors.
int i=0, index=-1;
Integer element;
Integer[] exampleArray = new Integer[25];
//Info.
System.out.println("**This shows you a bit about the Vector class as an alternative to arrays.**\n");
Thread.sleep(3500); //These just has the app wait for 3.5 seconds to let you catch up.
/* Declare the Vector.
* NOTE: Primitives cannot be used; use autoboxing/autounboxing & wrappers instead (like Integer, below)
*/
Vector<Integer> exampleVector = new Vector<Integer>();
//We haven't initialized the Vector yet, so it's empty. See for yourself:
if(exampleVector.isEmpty()) System.out.println("Yup, it's empty alright.\n");
Thread.sleep(3500);
//Now we'll add to the Vector. Let's get it done quickly by using a for loop to add 10 items:
System.out.print("Here are the elements we're adding: \n");
for(i=0; i<10; i++) {
exampleVector.addElement(i);
System.out.print(i + " ");
}
System.out.println("\n");
Thread.sleep(3500);
/* But wait! I want to add 100 to the 4th position (index 3). If this were an array, we'd be running into a few problems here,
* but since this is a Vector, it's as easy as: */
exampleVector.insertElementAt(100, 3);
//Now, check it out (also notice the for condition's operator ( .size() ):
for (i=0; i<exampleVector.size(); i++) {
System.out.print(exampleVector.elementAt(i) + " "); //Also notice how we get at elements: the elementAt(index) dot-operator.
}
System.out.println("\n");
Thread.sleep(3500);
//We can check if the Vector contains a specific element anywhere by using the .contains(Object) method:
System.out.println("Is the number 2 in the Vector?");
if(exampleVector.contains(2) == true) System.out.println("--> Yup.\n");
else System.out.println("--> Nope.\n");
Thread.sleep(3500);
//Ok. So now we know an element is present. What if I want to know its index so I can change it? It's this easy:
index = exampleVector.indexOf(2);
System.out.println("2 is at the index " + index + "\n");
Thread.sleep(3500);
/* Actually, I've decided that I don't want 2 in the list anymore. I want to delete it.
* Again, if this were an array, this would present some problems, but with a Vector, all we do is: */
exampleVector.removeElement(2);
//We also could have used the following code: exampleVector.removeElementAt(exampleVector.indexof(2)); -- they're equivalent.
//Let's see what happened:
for (i=0; i<exampleVector.size(); i++) {
System.out.print(exampleVector.elementAt(i) + " ");
}
System.out.println("\n");
Thread.sleep(3500);
//**The really cool thing about Vectors is that their actual size is dynamic. That element we removed
// actually shortened the whole Vector. Arrays are fixed in size once they are declared - inconvenient, huh?
//Of course, there are tons of other Vector methods; I suggest you head over to the API & check them out. Here are a couple:
//firstElement:
System.out.println("What's the first element in the Vector?");
element = exampleVector.firstElement();
System.out.println("--> " + element);
//lastElement:
element = exampleVector.lastElement();
System.out.println("What's the last element?\n--> " + element + "\n");
Thread.sleep(3500);
//If, for some weird reason, you really need an array, you can copy your Vector into an array like this:
exampleVector.copyInto(exampleArray);
//Let's see if it's really there:
for(i=0; i< 25; i++) { //25 is the length of the array above.
if(exampleArray[i] != null) System.out.print(exampleArray[i] + " ");
}
System.out.println("\n");
Thread.sleep(3500);
//Lastly, we can clear the Vector to end with what we started with like this:
exampleVector.removeAllElements();
System.out.println("Empty?");
if(exampleVector.isEmpty() == true) System.out.println("Yup, it is.\n");
else System.out.println("Nope. \n");
Thread.sleep(3500);
System.out.println("Well, that's the end of this tutorial/snippet. Hope you learned a bit about the advantages of using Vecotrs." +
"\nIf you're interested in learning more, you should check out the API to unlock all the Vector class' possibilities in your apps.!");
}
}
0 komentar:
Posting Komentar