将数组代码更改为向量
因此,我一直在尝试查看所有内容,但似乎无法找到将数组转换为向量所需更改的全部内容。或者更确切地说,我想我只是不知道到底要搜索什么。
数组是这样的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将数组打包成一个向量,这样:
然后您的循环就变成
我会使用
ArrayList
而不是Vector
除非您有令人信服的理由使用Vector< /code> 不过。
You can pack your array into a vector thus:
and then your loop becomes
I would use
ArrayList
overVector
unless you've got a compelling reason to useVector
though.这是使用向量的示例。我强烈建议使用 ArrayList 类,尽管 Vector 已经不再使用了。
Here is your example using a vector. I would highly recommend using the ArrayList class though as Vector is not used much anymore.
您可以使用 Arrays.asList 将数组转换为列表。 Vector 是一个列表,但我怀疑您需要的只是一个列表。最好对 List 接口进行编程,而不是对 Vector 等具体类进行编程。
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.亚历克斯,
如果您的代码不需要在多线程环境中安全,正如每个人建议的那样,您应该使用 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 hereLastly, 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.