0

So I have been trying to look all over, but can't seem to find a whole lot on what exactly needs to be changed in order to convert an array to vector. Or rather, I suppose I just don't know what exactly to search for.

The array is this:

public class Member{

 private int id;
 private String name;
 private String[] books = new String[5];
 //replace the String books array with a Vector variable named books
 //that functions exactly the same way 

 public Member() {
     id = 0;
     name = "John Doe";
 }

 public Member(int pId, String pName) {
     id = pId;
     name = pName;
 }

 public int getId(){
     return id;
 }

 public void setId(int pId){
     id=pId;
 }

 public String getName(){
     return name;
 }

 public void setName(String pName){
     name=pName;
 }

 public String[] getBooks(){
     return books;
 }

 public void setBooks(String[] pBooks){
     books=pBooks;
 }

 public String toString(){
     StringBuffer buf = new StringBuffer();
     buf.append("ID: " + getId() + "  Member: " + getName() + "\n");
     for(int x=0; x<books.length; x++ ) {
         buf.append("Book: " + books[x] + "\n");
     }
     return buf.toString();
 }


/**
 * @param args
 */
public static void main(String[] args) {
    String[] list = {"Java Data Structures", "The Bible", "Grapes of Wrath",
                    "Goldfinger", "Sam I Am", "The Cat in the Hat", "Shawshenk Redemption",
                    "Green Eggs and Ham", "Linus and Lucy", "Abraham Lincoln"};

    Member m1 = new Member();
    m1.setId(431);
    m1.setName("William Wallace");      
    for(int i=0; i<5; i++) {
        m1.books[i] = list[i]; 
    }

    Member m2 = new Member(7010, "Bonny Clyde");
    for(int i=5; i<list.length; i++) {
        int x = i-5;
        m2.books[x] = list[i]; 
    }

    System.out.println(m1.toString());
    System.out.println();
    System.out.println(m2.toString());
}

}

Now, I have been trying to convert it, but to be honest, I don't know what I am doing.

import java.util.Vector;

public class Member{

 private int id;
 private String name;
 private Vector<String> books = new Vector<String>(5);
 //replace the String books array with a Vector variable named books
 //that functions exactly the same way

 public Member() {
     id = 0;
     name = "John Doe";
 }

 public Member(int pId, String pName) {
     id = pId;
     name = pName;
 }

 public int getId(){
     return id;
 }

 public void setId(int pId){
     id=pId;
 }

 public String getName(){
     return name;
 }

 public void setName(String pName){
     name=pName;
 }

 public String getBooks(){
     return books;
 }

 public void setBooks(String pBooks){
     books=pBooks;
 }

 public String toString(){
     StringBuffer buf = new StringBuffer();
     buf.append("ID: " + getId() + "  Member: " + getName() + "\n");
     for(int x=0; x<books.length; x++ ) {
         buf.append("Book: " + books<x> + "\n");
     }
     return buf.toString();
 }


/**
 * @param args
 */
public static void main(String args) {
    String list = {"Java Data Structures", "The Bible", "Grapes of Wrath",
                    "Goldfinger", "Sam I Am", "The Cat in the Hat", "Shawshenk Redemption",
                    "Green Eggs and Ham", "Linus and Lucy", "Abraham Lincoln"};

    Member m1 = new Member();
    m1.setId(431);
    m1.setName("William Wallace");
    for(int i=0; i<5; i++) {
        m1.books<i> = list<i>;
    }

    Member m2 = new Member(7010, "Bonny Clyde");
    for(int i=5; i<list.length; i++) {
        int x = i-5;
        m2.books<x> = list<i>;
    }

    System.out.println(m1.toString());
    System.out.println();
    System.out.println(m2.toString());
}

}

Any help would be greatly appreciated.

1
  • Vector is really outdated. I suggest using an ArrayList instead. Anyway you obviously have a lot of compile errors. Have you read a tutorial on using Collections? Have you tried looking at each of your compile errors? Commented May 17, 2011 at 1:33

4 Answers 4

1

You can pack your array into a vector thus:

Vector<String> v = new Vector<String>(Arrays.asList(myList))

and then your loop becomes

for (String s : v) {
  ...
}

I would use ArrayList over Vector unless you've got a compelling reason to use Vector though.

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, but I would just use Arrays.asList(somearray) directly. Usualy, wrapping it with an arraylist doesn't buy you much. The only real interesting thing there is that arraylist can me modified and the list from asList() isn't (because it is backed by the array itself).
@Jeff, I agree that a simple wrapper is the way to go if you're OK with returning an immutable list. I wrapped it in a Vector because that was the original question. Besides that, converting the array to an ArrayList or Vector can help in two ways: defensive copying, clients can modify it without mucking with the original array; and support for mutation, specifically remove() and add().
1

Here is your example using a vector. I would highly recommend using the ArrayList class though as Vector is not used much anymore.

import java.util.Vector;

public class Member{

 private int id;
 private String name;
 private Vector<String> books = new Vector<String>();
 //replace the String books array with a Vector variable named books
 //that functions exactly the same way

 public Member() {
     id = 0;
     name = "John Doe";
 }

 public Member(int pId, String pName) {
     id = pId;
     name = pName;
 }

 public int getId(){
     return id;
 }

 public void setId(int pId){
     id=pId;
 }

 public String getName(){
     return name;
 }

 public void setName(String pName){
     name=pName;
 }

 public Vector<String> getBooks(){
     return books;
 }

 public void add(String book) {
    books.add(book);
 }


 public void setBooks(Vector<String> pBooks){
     books=pBooks;
 }

 public String toString(){
     StringBuffer buf = new StringBuffer();
     buf.append("ID: " + getId() + "  Member: " + getName() + "\n");
     for(int x=0; x<books.size(); x++ ) {
         buf.append("Book: " + books.get(x) + "\n");
     }
     return buf.toString();
 }


/**
 * @param args
 */
public static void main(String[] args) {
    String[] list = new String[]{"Java Data Structures", "The Bible", "Grapes of Wrath",
                    "Goldfinger", "Sam I Am", "The Cat in the Hat", "Shawshenk Redemption",
                    "Green Eggs and Ham", "Linus and Lucy", "Abraham Lincoln"};

    Member m1 = new Member();
    m1.setId(431);
    m1.setName("William Wallace");
    for(int i=0; i<5; i++) {
        m1.add(list[i]);
    }

    Member m2 = new Member(7010, "Bonny Clyde");
    for(int i=5; i<list.length; i++) {
        int x = i-5;
        m2.add(list[x]);
    }

    System.out.println(m1.toString());
    System.out.println();
    System.out.println(m2.toString());
}

}

3 Comments

You should get rid of the Hungarian notation, which is frowned upon in Java. Also you might want to consider changing get/setBooks' type to List instead of Vector. It's always better to program to an interface than a concrete class.
I agree on both points. However, I didn't want to rewrite his code except to solve the question he was asking.
I should also note, that based on the nature of his question, he is obviously new to Java and I didn't want to confuse the matter further, hence why I suggested using ArrayList and not getting into the programming to interfaces suggestion.
0

You can use Arrays.asList to convert an array to a list. Vector is a List but I suspect all you need is a list. It's a good idea to program to the List interface rather than a concrete class such as Vector.

List<String> list = Arrays.asList(array)

Comments

0

Alex, If your code does not need to be safe in a multi threaded environment, as everyone suggested you should use ArrayList.

In case, you have concurrent operations, you may want to have a look at Collections.synchronizedList. However, do check some of the limitations of using that as given here

Lastly, there are some situations where vector is necessary. There I would recommended Vector<String> v = new Vector<String>(Arrays.asList(myList)) as suggested by Mike earlier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.