java中的Vectors / Generics

时间:2014-04-14 18:23:54

标签: java generics vector

我正在尝试创建一个类,这样当我输入一些单词(例如名称)时,只有具有HighRights权限的计算机才会打印出来。我正在努力从我的ex6课程中访问该向量。

import java.util.*;
import java.lang.*;
@SuppressWarnings( "unchecked" ) public class Ex6 {

    public static void main(String[] a)
    {
       int i=Integer.parseInt(a[0]);
       if (i==0) {
           Vector ma = new Vector();
          ma.add(new HighRights("Jimmy"));
          ma.add(new HighRights("Jim"));
          ma.add(new HighRights("Mark"));
          ma.add(new HighRights("John"));
          ma.add(new LowRights("Lisa") );
           System.out.println(new Ex6().CountHighs(ma));
        }
         if (i==1) {
           Vector ma = new Vector();
          ma.add(new HighRights("Jimmy"));
          ma.add("I'm not Jimmy" );
           System.out.println(new Ex6().CountHighs(ma));
        }
        if (i==2)  System.out.println(new Ex6().CountHighs(null));

    }
    public static int CountHighs (Vector m) {
      int count = 0;

        try{
            for(int i = 0; i < m.size(); i++)
            {
                if(m.get(i) instanceof HighRights)
                {
                    count++;
                }

            }
        }
        catch(NullPointerException e){
             System.out.println("Error");
             System.exit(0);
             return 0;

      }
      return count;
    }
}

这是我希望能够检查他们是在高级还是低级的课程。

import java.util.*;
 public class Ex8 {

    public void printHighUsers(ArrayList<SecurityRights> a){

    }

     public static void main(String[] args)
{

      Vector ma = new Vector();
      ma.add(new HighRights("Jimmy"));
      ma.add(new HighRights("Jim"));
      ma.add(new HighRights("Mark"));
      ma.add(new LowRights("John"));
      ma.add(new LowRights("Lisa") );

     }
    }
}

其他一些可能有用的相关信息,SecruityRights是HighRights和LowRights都可以扩展的超类。我怀疑这3个课程中的任何信息都会有所帮助,所以没有在这里发布。

这是我想要做的一个例子,因为我在顶部的解释可能已经糟透了:

例如,如果ArrayList a包含三个SecurityRights对象,则第一个是LowRights对象 命名为“John”,第二个名为HighRights,名称为“Mary”,第三个名为HighRights,名称为“Keith”,然后: printHighUsers(a)将在屏幕上打印:“Mary Keith”

2 个答案:

答案 0 :(得分:0)

您应该使用类似&lt; SecurityRights&gt;这样的泛型类型声明Vector实例,这也是 在添加String时显示问题。

Ex8应该使用List&lt;?扩展SecurityRights&gt;因此,如果您只需要浏览元素,就可以传递任何列表,甚至是Iterable。

答案 1 :(得分:0)

import java.util.*;
public class Ex8 {

    private Vector ma;
    public Ex8() {
      ma = new Vector<SecurityRights>();
    }
    public Vector getMa(){
      return ma;
    }
    public void printHighUsers(ArrayList<SecurityRights> a){
          //check if vector contains any HighRights users from arraylist a.
    }

    public static void main(String[] args)
    {
           Ex8 example = new Ex8();
           Vector<SecurityRights> ma = example.getMa();
           ma.add(new HighRights("Jimmy"));
           ma.add(new HighRights("Jim"));
           ma.add(new HighRights("Mark"));
           ma.add(new LowRights("John"));
           ma.add(new LowRights("Lisa") );
           example.printHighUsers(/*your ArrayList*/)
    }
}