简体   繁体   中英

Having trouble in printing the largest array number from the 20 randomly generated numbers

Ok so I been working on this assignment all day for the past 3 days but I haven't had any luck. I wasn't going to ask for help but I finally gave up. But there is also one more thing I need to implement to the code. This is what I gotta implement "Find the length of the longest continuous series of positive numbers in the array data. If the contents were: 4 5 0 2 . . . -1 88 78 66 -6. The length would be 3. For this problem, 0 is considered non-negative but not positive". Plus I have an issue where I can't print the largest int in the array of 20.

import java.util.Random;
import java.util.ArrayList;
public class arrayops {

public static int findLargest(ArrayList<Integer> nums) {
      int greatestnum = nums.get(0);
      for (Integer item : nums) {
          if (item > greatestnum) {
              greatestnum = item;
          }
      }
      return greatestnum; 
  }
  public static int randomData(ArrayList<Integer> nums) {
      int[] array = new int [20];
      Random random = new Random();
      for (int i = 0; i < array.length; i++) {
          array[i] = -100 + random.nextInt(201);
      }
      return -100 + random.nextInt(201);
  }

  public static void main(String[] args) {
      ArrayList<Integer> nums = new ArrayList<Integer>();
      nums.add(1);
      nums.add(4);
      nums.add(13);
      nums.add(43);
      nums.add(-25);
      nums.add(17);
      nums.add(22);
      nums.add(-37);
      nums.add(29);
      System.out.println("The Greatest Number from the hardcoded numbers " + findLargest(nums));
      System.out.println("The Greatest number from the random numbers " + randomData(nums));
     }
    } 

The findLargest method:

public static int findLargest(ArrayList<Integer> nums) {
  int greatestnum = 0;
  int greatestLen = 0;
  for (Integer item : nums) {
      if (item > 0) {
          greatestLen++ ;
          if(greatestLen > greatestnum)
              greatestnum = greatestLen;
      }
      else
          greatestLen = 0;
  }
  return greatestnum; 
}

Logic used:

  1. Keep the length of the longest chain encountered, and the length of current chain, in two separate variables (greatestnum and greatestLen respectively)

  2. Increment greatestLen every time a positive number is encountered. If the number if less than or equal to zero, reset this count.

  3. If the length of current chain is greater than the previous longest chain, sent the longest chain size to current chain size.

The problem is you created a list with random numbers but never put that list into the findLargest method. You also never created a method to find the consecutive positive numbers. If you didn't know how to go about coding it, I recommend drawing out an algorithm on paper.

Largest value in ArrayList...

public static int findL(ArrayList<Integer> nums)
{
    int top = nums.get(0);
    for(int i = 0; i<nums.size(); i++)
    {
        if(nums.get(i)>top)
        {
            top = nums.get(i);
        }
    }
    return top;
}

Largest number of consecutive positives...

public static int positiveString(ArrayList<Integer> nums)
{
    int longest = 0;
    int count = 0;
    for(int i = 0; i<nums.size(); i++)
    {
        if(nums.get(i) > 0)
        {
            count++;
        }
        else
        {
            if(longest<count)
            {
                longest = count;
            }
            count = 0;
        }
    }
    return longest;
}

If you want to arrange the numbers into order you can simply use java.util.TreeSet . Then use the method last() to get the largest number.

public static int findLargest(ArrayList<Integer> nums) {
    return new TreeSet<Integer>(nums).last();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM