Java 排序数组列表

Java 排序数组列表,java,sorting,arraylist,Java,Sorting,Arraylist,我希望按公司名称对数组进行排序,只打印在特定时间之前开放的公司。。。。。。印刷前 public void printCompanies(int time) { Iterator <Company> it = CompanyList.iterator(); while( it.hasNext()) { Company a = it.next(); if(a.getTime() == (time)) { Collec

我希望按公司名称对数组进行排序,只打印在特定时间之前开放的公司。。。。。。印刷前

public void printCompanies(int time)
{
    Iterator <Company> it = CompanyList.iterator();
    while( it.hasNext()) {
        Company a = it.next();
        if(a.getTime() == (time)) {
            Collections.sort(CompanyList.getName());
        }
    }
}

然后我得到一个错误“无法从静态上下文引用非静态方法getName()”

简短的回答是,不,您的路径不对;-)

答案的较长版本是,你不能打电话给名单上的某个成员,你需要打电话给名单上的某个成员

默认情况下,
Collections.sort
将使用成员的自然排序,如果您想更改需要传入自己的比较器的排序

一旦对列表进行了排序,就可以对其进行迭代以对其进行过滤,并对结果执行一些处理

因此,您希望首先对列表进行排序,然后对其进行筛选,例如:

public void printCompanies(int time)
{
  Collections.sort(companyList, 
                   new Comparator() {
                     public int compare(Object o1, Object o2) {
                       if (o1 == o2)
                         return 0;
                       if (o1 == null)
                         return -1;
                       if (o2 == null)
                         return 1;
                       Company c1 = (Company) o1;
                       Company c2 = (Company) o2;
                       return c1.getName().compareTo(c2.getName());
                     }
                   });
  Iterator <Company> it = companyList.iterator();
  while(it.hasNext()) {
    Company a = it.next();
    if(a.getTime() <= (time)) {
      // Do something
    }
  }
}
上市公司(整数时间)
{
Collections.sort(公司列表,
新比较器(){
公共整数比较(对象o1、对象o2){
如果(o1==o2)
返回0;
如果(o1==null)
返回-1;
如果(o2==null)
返回1;
公司c1=(公司)o1;
公司c2=(公司)o2;
返回c1.getName().compareTo(c2.getName());
}
});
Iterator it=companyList.Iterator();
while(it.hasNext()){
公司a=it.next();

如果(a.getTime()简短的回答是:不,您走的路不对;-)

答案的较长版本是,你不能打电话给名单上的某个成员,你需要打电话给名单上的某个成员

默认情况下,
Collections.sort
将使用成员的自然排序,如果您想更改需要传入自己的比较器的排序

一旦对列表进行了排序,就可以对其进行迭代以对其进行过滤,并对结果执行一些处理

因此,您希望首先对列表进行排序,然后对其进行筛选,例如:

public void printCompanies(int time)
{
  Collections.sort(companyList, 
                   new Comparator() {
                     public int compare(Object o1, Object o2) {
                       if (o1 == o2)
                         return 0;
                       if (o1 == null)
                         return -1;
                       if (o2 == null)
                         return 1;
                       Company c1 = (Company) o1;
                       Company c2 = (Company) o2;
                       return c1.getName().compareTo(c2.getName());
                     }
                   });
  Iterator <Company> it = companyList.iterator();
  while(it.hasNext()) {
    Company a = it.next();
    if(a.getTime() <= (time)) {
      // Do something
    }
  }
}
上市公司(整数时间)
{
Collections.sort(公司列表,
新比较器(){
公共整数比较(对象o1、对象o2){
如果(o1==o2)
返回0;
如果(o1==null)
返回-1;
如果(o2==null)
返回1;
公司c1=(公司)o1;
公司c2=(公司)o2;
返回c1.getName().compareTo(c2.getName());
}
});
Iterator it=companyList.Iterator();
while(it.hasNext()){
公司a=it.next();

如果(a.getTime()简短的回答是:不,您走的路不对;-)

答案的较长版本是,你不能打电话给名单上的某个成员,你需要打电话给名单上的某个成员

默认情况下,
Collections.sort
将使用成员的自然排序,如果您想更改需要传入自己的比较器的排序

一旦对列表进行了排序,就可以对其进行迭代以对其进行过滤,并对结果执行一些处理

因此,您希望首先对列表进行排序,然后对其进行筛选,例如:

public void printCompanies(int time)
{
  Collections.sort(companyList, 
                   new Comparator() {
                     public int compare(Object o1, Object o2) {
                       if (o1 == o2)
                         return 0;
                       if (o1 == null)
                         return -1;
                       if (o2 == null)
                         return 1;
                       Company c1 = (Company) o1;
                       Company c2 = (Company) o2;
                       return c1.getName().compareTo(c2.getName());
                     }
                   });
  Iterator <Company> it = companyList.iterator();
  while(it.hasNext()) {
    Company a = it.next();
    if(a.getTime() <= (time)) {
      // Do something
    }
  }
}
上市公司(整数时间)
{
Collections.sort(公司列表,
新比较器(){
公共整数比较(对象o1、对象o2){
如果(o1==o2)
返回0;
如果(o1==null)
返回-1;
如果(o2==null)
返回1;
公司c1=(公司)o1;
公司c2=(公司)o2;
返回c1.getName().compareTo(c2.getName());
}
});
Iterator it=companyList.Iterator();
while(it.hasNext()){
公司a=it.next();

如果(a.getTime()简短的回答是:不,您走的路不对;-)

答案的较长版本是,你不能打电话给名单上的某个成员,你需要打电话给名单上的某个成员

默认情况下,
Collections.sort
将使用成员的自然排序,如果您想更改需要传入自己的比较器的排序

一旦对列表进行了排序,就可以对其进行迭代以对其进行过滤,并对结果执行一些处理

因此,您希望首先对列表进行排序,然后对其进行筛选,例如:

public void printCompanies(int time)
{
  Collections.sort(companyList, 
                   new Comparator() {
                     public int compare(Object o1, Object o2) {
                       if (o1 == o2)
                         return 0;
                       if (o1 == null)
                         return -1;
                       if (o2 == null)
                         return 1;
                       Company c1 = (Company) o1;
                       Company c2 = (Company) o2;
                       return c1.getName().compareTo(c2.getName());
                     }
                   });
  Iterator <Company> it = companyList.iterator();
  while(it.hasNext()) {
    Company a = it.next();
    if(a.getTime() <= (time)) {
      // Do something
    }
  }
}
上市公司(整数时间)
{
Collections.sort(公司列表,
新比较器(){
公共整数比较(对象o1、对象o2){
如果(o1==o2)
返回0;
如果(o1==null)
返回-1;
如果(o2==null)
返回1;
公司c1=(公司)o1;
公司c2=(公司)o2;
返回c1.getName().compareTo(c2.getName());
}
});
Iterator it=companyList.Iterator();
while(it.hasNext()){
公司a=it.next();

如果(a.getTime()像其他人所说的那样,您需要在迭代之前进行一次排序。但是,要按名称进行排序,您需要做的是更改
Company
类以实现
Comparable
接口(首选解决方案)或者在迭代时添加一个内部类,该类实现了
Comparator
。无论采用哪种方式,都会在接口强制实现的方法中调用类似于
companyA.getName().compareTo(companyB.getName())
的东西

如果您选择第一条路线,则只需调用
Collections.sort(l