3 Replies - 1460 Views - Last Post: 12 February 2010 - 06:28 AM Rate Topic: -----

#1 MacNix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 09

Java Vectors, Beans and Store

Posted 11 February 2010 - 11:03 AM

Hi

I am doing a course in Object Orientated Programming.

I am under a great deal of confusion when it comes to Vectors and have been set as homework creating vectors and beans.

What my lecturer explained to me is that basically your vector is your filing cabinet and a bean is a bit of paper with info held in it and placing that bit of paper into another filing cabinet. I can understand that part what I cant understand is how to write the code. What my lecturer has not done is shown me in code an example of it and I feel that until I see that and have it explained to me that I will not get any more further with doing it.

I am looking all over the internet and getting more confused! What I really want to know is how do you write a Vector.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Vectors, Beans and Store

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Java Vectors, Beans and Store

Posted 11 February 2010 - 11:14 AM

I googled "Java vector" and got:

http://leepoint.net/...ts/vectors.html
http://java.sun.com/...til/Vector.html
http://www.javacoffe...aq/faq0017.html

All of which break vectors down pretty well. Vectors are collections of the same type of data. Same thing as an arraylist or an array. The big difference between an array and a vector is, with an array you tell it to be a certain size and it stays that size, a vector will grow when u keep adding more items

Vector v = new Vector(); //default size is 10 internally
for(int i = 0; i < 15; i++ ){
 v.add( i );
}


Was This Post Helpful? 1
  • +
  • -

#3 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Java Vectors, Beans and Store

Posted 11 February 2010 - 11:20 AM

Well the code is the easy part, so you are in luck!
vectors are part of the java.util package so make sure u import them!

To declare a vector you type

Vector<Class type to hold> name = new Vector<Class type to hold>();

The code below shows this for Strings and Integers.

You would probably want to make a bean class and then use:
Vector<bean> beans = new Vector<bean>();

good luck! i hope this helps

import java.util.Vector;

public class TestClass {

	public static void main(String[] args)  {
		
		Vector<String> strings = new Vector<String>();
		
		strings.add("Hello ");
		strings.add("world ");
		strings.add("this ");
		strings.add("is ");
		strings.add("your ");
		strings.add("father ");
		strings.add(":0 ");
		strings.add("\n");
		
		for(int i=0;i<strings.size();i++){
			System.out.print(strings.get(i));
		}
		for(String x : strings){
			System.out.print(x);
		}
		
		Vector<Integer> bestYears = new Vector<Integer>();
		bestYears.add(1990);
		bestYears.add(2010);
		bestYears.add(1320);
		
		for(int i : bestYears){
			System.out.println(i);
		}
		
	}
}


This post has been edited by gl3thr0: 11 February 2010 - 11:20 AM

Was This Post Helpful? 1
  • +
  • -

#4 MacNix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 09

Re: Java Vectors, Beans and Store

Posted 12 February 2010 - 06:28 AM

Thanks guys for your help. It is much clearer now.

I have another question as this is another thing my lecturer has asked us to look at! If you wanted to have multiple vectors within one vector, how would you code that? Would it be the same as in your example of a string and best years?

So just to get this clear in head, for example if you had a bean class for Student, a vector for the values of Student would there be another class if you wanted multiple student values stored or is this unnecessary as the vector already stores those values?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1