【问题标题】:How compare int[] elements in arraylist?如何比较数组列表中的 int[] 元素?
【发布时间】:2021-03-24 08:21:52
【问题描述】:
public company(String a, int[] b)

我创建了一个类,company

ArrayList<company> c1;
int temp;
for(company c:c1) {
    if (temp > c.getb()) {
        temp = c.getb();
    }

有一个公司列表,一个公司有许多整数作为int[]存储在其中。

我正在尝试比较并从int[] b 中获取最大值。

但我的代码显示错误,因为 c.getb()int[] 数组,因此无法通过 &lt;&gt; 进行比较

所以,我不确定如何比较 ArrayList 中的 int[] 值。

【问题讨论】:

    标签: java arrays arraylist compare


    【解决方案1】:

    我正在尝试比较并从 int[] b 中获取最大值。

    您的代码有几个问题:

    ArrayList<company> c1 = ...
    int temp;
    for(company c : c1) {
        if (temp > c.getb()) {
            temp = c.getb();
        }
    }
    

    首先,您需要将temp 初始化为一个值,哪个值? Integer.MIN_VALUE;,因为您正试图获得 最大 值。此外,这个:

    for(company c : c1) {
        if (temp > c.getb()) {
            temp = c.getb();
        }
    }
    

    没有得到最大的,而是得到最小的。所以你真正想做的是:

    List<company> c1 = ....
    int max = Integer.MIN_VALUE;
    for(company c : c1) {
        int temp = Arrays.stream(c.getb()).max().getAsInt();
        max = Math.max(temp, max);
    }
    

    如果您不想使用Arrays.stream,您可以自己创建函数:

    public static int maxArray(int [] array){
        int max = Integer.MIN_VALUE;
        for(int e :  array) {
            max = Math.max(e, max);
        }
        return max;
    }
    

    如您所见,这与您最初尝试做的事情非常相似。

    使用 Java 流,您可以执行以下操作:

    ArrayList<company> c1 = ...
    int max = c1.stream()
                .map(c -> Arrays.stream(c.getb()).max().getAsInt())
                .max(Comparator.naturalOrder())
                .orElse(Integer.MIN_VALUE);
    

    【讨论】:

      【解决方案2】:

      您可以使用原生 java 方法获取具有原语的数组的最大值:

      Collections.max(Arrays.asList(array));

      为了节省性能,我们将保存列表。此外,您无法将 temp 与某些东西进行比较,因为您没有初始化 temp。

      如果您想获得更大的数字,您会想要使用temp &lt; max 而不是temp &gt; max,因此每次迭代您都会检查公司元素的 b 数组是否比其前任具有更大的数字。

      List<company> c1 = ....
      int temp = Integer.MIN_VALUE;
      for(company c:c1) {
          int max = Collections.max(Arrays.asList(c.getb()));
          if (temp < max) {
              temp = max;
          }
      }
      

      【讨论】:

        【解决方案3】:
        
        import java.util.ArrayList;
        
        public class Runner {
        
            public static void main(String[] args) {
        
                // The arraylist of company
                ArrayList<Company> c1 = new ArrayList<>();
                
                // First set of data creating the int[] for Apple company and inserting in c1 List
                int[] i = { 9, 4, 7, 5, 10, 8 };
                c1.add(new Company("Apple", i));
                
                // Second set of data creating the int[] for Samsung company and inserting in c1 List
                i = new int[] { 0, 2, -3, 4, 5 };
                c1.add(new Company("Samsung", i));
        
                // The arraylist has now 2 companies
                
                
                // First we store the largest from the array of the first company in temp
                int temp = Runner.findLargestFromArray(c1.get(0).getB());
                
                // We iterate the arraylist of the companies
                for (Company c : c1) {
                    // For each company we find the largest of int[] B 
                    int l = Runner.findLargestFromArray(c.getB());
                    
                    // If temp is less than the largest it will be updated
                    if (temp < l) {
                        temp = l;
                    }
        
                }
                // the largest of arrays of both the companies is stored here i.e 10
                System.out.println(temp);
            }
        
            // Method which takes array as an input and returns the highest number stored in it
            public static int findLargestFromArray(int[] arr) {
        
                int largest = arr[0];
                int index = 1;
                while (index < arr.length) {
                    // check if largest is smaller than element
                    if (largest < arr[index]) {
                        // update largest
                        largest = arr[index];
                    }
                    index++;
                }
                return largest;
            }
        
        }
        

        主驱动代码

        package mpackage;
        
        import java.util.Arrays;
        
        public class Company {
            String a;
            int[] b;
            
            
            public Company() {
            }
        
        
            public Company(String a, int[] b) {
                this.a = a;
                this.b = b;
            }
        
        
            public String getA() {
                return a;
            }
        
        
            public void setA(String a) {
                this.a = a;
            }
        
        
            public int[] getB() {
                return b;
            }
        
        
            public void setB(int[] b) {
                this.b = b;
            }
        
        
            @Override
            public String toString() {
                return "Company [a=" + a + ", b=" + Arrays.toString(b) + "]";
            }
        
            
        }
        

        公司类

        【讨论】:

          【解决方案4】:

          试试这个。

          ArrayList<company> c1 = new ArrayList<>();
          // ....
          int temp = c1.stream()
              .flatMapToInt(c -> Arrays.stream(c.getB()))
              .max().getAsInt();
          

          当列表为空时,没有最大值,所以抛出NoSuchElementException

          【讨论】:

          • 如果流为空,建议使用.orElse(0),而不是.getAsInt()
          • @Lino 唯一的问题是零实际上可能是一个有效的最大值
          • @dreamcrash 同意,这真的取决于输入数据,所以Integer.MIN_VALUE 可能更适合
          猜你喜欢
          • 1970-01-01
          • 2020-08-15
          • 1970-01-01
          • 1970-01-01
          • 2015-08-01
          • 2011-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多