ArrayLists - 加在一起

时间:2015-05-27 21:15:42

标签: arrays arraylist merge

您好我正在尝试将这两个数组列表合并到驱动程序类中的堆栈类之外;我不太确定如何去做。我知道addall方法,如果它在同一个类中。这是我尝试过的方式。

package javaapplication1;
public class JavaApplication1 {

public static void main(String[] args) {

    Stack stack = new Stack();
    stack.push(new Person("jeffery", "1866"));
    stack.push(new Person("rachel", "1221"));
    stack.push(new Person("amy", "2390"));
    stack.push(new Person("tom", "1943"));
    stack.push(new Person("jimbo", "11199"));
    stack.push(new Person("anna", "1250"));
    stack.push(new Person("tammy", "1800"));
    stack.push(new Person("john", "1120"));
    stack.PrintAndEmpty();

    Stack stack2 = new Stack();
    stack2.push(new Person("jillian", "1866"));
    stack2.push(new Person("jentry", "1221"));
    stack2.push(new Person("america", "2390"));
    stack2.push(new Person("timmy", "1943"));
    stack2.push(new Person("bacon", "11199"));
    stack2.push(new Person("laura", "1250"));
    stack2.push(new Person("angus", "1800"));
    stack2.push(new Person("jimmy", "1120"));
    stack2.PrintAndEmpty();

    Stack stack3 = new Stack();
    stack3.push(new Person(stack , stack2));
    System.out.println(stack, stack2);

    }


}

package javaapplication1;

import java.util.*;

public class Stack<E> {

private ArrayList<E> head;

public Stack() {
    head = new ArrayList<E>();
}

public Stack(int initialCapacity) {
//constructor... creates intitial stack
    head = new ArrayList<E>(initialCapacity);
}

public void push(E x) {
    head.add(x);
}

public E pop() {
    if (empty()) {
        return null;
    }
    return head.remove(head.size() - 1);
}

public boolean empty() {
    return head.isEmpty();
}

public int size() {
    return head.size();
}

public E peek() {
    if (empty()) {
        return null;
    }
    return head.get(head.size());
}

public void PrintAndEmpty() {
    while (!this.empty()) {
        System.out.println(pop().toString());
    }
    System.out.println("");

}
}

 package javaapplication1;


public class Person {

private String name;
private String identification;

public Person() {
    name = "";
    identification = "";
}

public Person(String n, String idNum) {
    name = n;
    identification = idNum;
}

public String getName() {
    return name;
}

public String getID() {
    return identification;
}

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

public void setID(String idNum) {
    identification = idNum;
}

public boolean equals(Object o) // name and id are the same
            {
    return ((((Person) o).name).equals(name)
            && (((Person) o).identification).equals(identification));
}

@Override
public String toString() {
return "Person{ + name  + identification   + '}';
    }
}

这是我刚刚尝试过的,似乎无法正常工作

public static ArrayList merge(stack,stack2){

 Stack merged = new Stack();      
 merged.push(stack,stack2);
}
enter code here

0 个答案:

没有答案