将数组代码更改为向量

发布于 2024-11-08 04:20:46 字数 3593 浏览 0 评论 0原文

因此,我一直在尝试查看所有内容,但似乎无法找到将数组转换为向量所需更改的全部内容。或者更确切地说,我想我只是不知道到底要搜索什么。

数组是这样的:

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());
}

}

现在,我一直在尝试转换它,但说实话,我不知道我在做什么。

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());
}

}

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-11-15 04:20:46

您可以将数组打包成一个向量,这样:

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

然后您的循环就变成

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

我会使用 ArrayList 而不是 Vector 除非您有令人信服的理由使用 Vector< /code> 不过。

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.

り繁华旳梦境 2024-11-15 04:20:46

这是使用向量的示例。我强烈建议使用 ArrayList 类,尽管 Vector 已经不再使用了。

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());
}

}

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());
}

}
七度光 2024-11-15 04:20:46

您可以使用 Arrays.asList 将数组转换为列表。 Vector 是一个列表,但我怀疑您需要的只是一个列表。最好对 List 接口进行编程,而不是对 Vector 等具体类进行编程。

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

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)
昔日梦未散 2024-11-15 04:20:46

亚历克斯,
如果您的代码不需要在多线程环境中安全,正如每个人建议的那样,您应该使用 ArrayList。

如果您有并发操作,您可能需要查看Collections.synchronizedList。但是,请检查使用此处给出的一些限制< /a>

最后,在某些情况下向量是必要的。在那里我会推荐 Vector; v = new Vector(Arrays.asList(myList)) 正如迈克之前所建议的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文