Arraylist 中的 Arraylist,未经检查的错误。爪哇

发布于 2024-12-22 17:01:52 字数 2856 浏览 2 评论 0原文

我试图将数组列表放入数组列表中。将数据添加到新数组中,然后按顺序打印它们。我只收到错误。

这是使用 for 循环在另一个数组列表中创建数组列表的正确方法吗?

我现在还想知道如何以比这些长表达式更好的方式从数组中获取数据。

我的错误

    jogging.java:101: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                res.get(iter).add(new Resultat(name,time));

   jogging.java:152: warning: [unchecked] unchecked conversion found   : java.util.ArrayList required: java.util.List<T> Collections.sort(res.get(iter2));

    jogging.java:152: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>) in java.util.Collections is applied to (java.util.ArrayList)
                Collections.sort(res.get(iter2));

    import java.util.;
    import java.lang.;

class Resultat implements Comparable<Resultat> { String namn; double tid; public Resultat( String n, double t ) { namn = n; tid = t; } public String toString() { return namn + " "+ tid; } public int compareTo( Resultat r ) { if (this.tid < r.tid){ return -1; } else if (this.tid > r.tid){ return 1; } else if (this.tid == r.tid && this.namn.compareTo(r.namn) <= 0) { return -1; } else if ( this.tid == r.tid && this.namn.compareTo(r.namn) >= 0){ return 1; } else {return 0;} } } public class jogging { public static void main( String[] args ){ int runners = scan.nextInt(); int competitions = scan.nextInt(); //create arraylist with arraylists within ArrayList <ArrayList> res = new ArrayList<ArrayList>(); for(int i = 0; i <= competitions; ++i){ res.add(new ArrayList<Resultat>()); } for (int i = 0; i < runners; i++){ String name = scan.next(); //runs the person made int antalruns = scan.nextInt(); for(int n = 0; n <antalruns; n++){ //number of the run int compnumber = scan.nextInt(); //time for the run double time = scan.nextDouble(); for(int iter = 0; iter < res.size(); ++iter){ res.get(iter).add(new Resultat(name,time)); } } } for(int iter2 = 0; iter2 < res.size(); ++iter2) { Collections.sort(res.get(iter2)); System.out.println(iter2); for(int it = 0; it < res.get(iter2).size(); ++it) { System.out.println(res.get(iter2).get(it)); } } } }

Im trying to put arraylists into an arraylist. Adding data to the new arrays, then print them in order. Im only getting errors.

Is this the right way to create an arraylist in another arraylist using a for loop?

I would also like to now how I can get data from the array in a better way then these long expresions.

My errors

    jogging.java:101: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                res.get(iter).add(new Resultat(name,time));

   jogging.java:152: warning: [unchecked] unchecked conversion found   : java.util.ArrayList required: java.util.List<T> Collections.sort(res.get(iter2));

    jogging.java:152: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>) in java.util.Collections is applied to (java.util.ArrayList)
                Collections.sort(res.get(iter2));

    import java.util.;
    import java.lang.;

class Resultat implements Comparable<Resultat> { String namn; double tid; public Resultat( String n, double t ) { namn = n; tid = t; } public String toString() { return namn + " "+ tid; } public int compareTo( Resultat r ) { if (this.tid < r.tid){ return -1; } else if (this.tid > r.tid){ return 1; } else if (this.tid == r.tid && this.namn.compareTo(r.namn) <= 0) { return -1; } else if ( this.tid == r.tid && this.namn.compareTo(r.namn) >= 0){ return 1; } else {return 0;} } } public class jogging { public static void main( String[] args ){ int runners = scan.nextInt(); int competitions = scan.nextInt(); //create arraylist with arraylists within ArrayList <ArrayList> res = new ArrayList<ArrayList>(); for(int i = 0; i <= competitions; ++i){ res.add(new ArrayList<Resultat>()); } for (int i = 0; i < runners; i++){ String name = scan.next(); //runs the person made int antalruns = scan.nextInt(); for(int n = 0; n <antalruns; n++){ //number of the run int compnumber = scan.nextInt(); //time for the run double time = scan.nextDouble(); for(int iter = 0; iter < res.size(); ++iter){ res.get(iter).add(new Resultat(name,time)); } } } for(int iter2 = 0; iter2 < res.size(); ++iter2) { Collections.sort(res.get(iter2)); System.out.println(iter2); for(int it = 0; it < res.get(iter2).size(); ++it) { System.out.println(res.get(iter2).get(it)); } } } }

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

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

发布评论

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

评论(2

魂ガ小子 2024-12-29 17:01:52

未经检查的警告是因为您尚未声明第二个 ArrayList 的泛型类型。尝试使用

ArrayList <ArrayList<Resultat>> res = new ArrayList<ArrayList<Resultat>>();

Yes,这有点乏味。 :-(

另外,大多数人认为最好在左侧使用接口(例如 List,而不是 ArrayList),以防万一您将来改变主意。例如

List <List<Resultat>> res = new ArrayList<ArrayList<Resultat>>();

ADDED

另外,您可以简化您的compareTo要比较 tid,请查看 Double.compare() 方法。

   public int compareTo( Resultat r ) {

      int compare = Double.compare(tid, r.tod);
      if (compare != 0)
         return compare;
      else
         return namn.compareTo(r.namn);
   }

The unchecked warnings are because you haven't declared the generic types of the 2nd ArrayList. Try using

ArrayList <ArrayList<Resultat>> res = new ArrayList<ArrayList<Resultat>>();

Yes, it's a bit tedious. :-(

Also, most think it good practice to use the interface (e.g. List, not ArrayList) on the left side, just in case you change your mind as to the implementation in the future. e.g.

List <List<Resultat>> res = new ArrayList<ArrayList<Resultat>>();

ADDED

Also, you could simplify your compareTo() method. For comparing the tids, look at Double.compare(). Something like:

   public int compareTo( Resultat r ) {

      int compare = Double.compare(tid, r.tod);
      if (compare != 0)
         return compare;
      else
         return namn.compareTo(r.namn);
   }
幸福%小乖 2024-12-29 17:01:52

每次创建 ArrayLIst 时,您都需要声明其数据类型。因此,当您在 ArrayList 中创建 ArrayLIst 时,您需要编写:

ArrayList<ArrayList<Resultat>> res = new ArrayList<ArrayList<Resultat>>();

如果您编写以下内容,编译器也必须付出更少的努力:

import java.util.ArrayList;  // NOT: import java.util.*;

类名从大写字母开始是 java 命名约定。因此,将类名写为:

public class Jogging {
    /..
}

Every time you create a ArrayLIst you need to declare its datatype. So when you create a ArrayLIst inside ArrayList you need to write:

ArrayList<ArrayList<Resultat>> res = new ArrayList<ArrayList<Resultat>>();

Also compiler has to put less effort if you write:

import java.util.ArrayList;  // NOT: import java.util.*;

Having a class name start from upperCase is java naming convention. So it is always a good practice to write your class name as:

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