-1

I'm running into an issue when I try to check if an input integer array is empty. The error message just reads: cannot find symbol.

If the prices array is not empty, I want to be able to run the rest of the code.

If it is, I want it to return 0.

import java.util.ArrayList;
import java.util.Collections;
class Solution {
    public int maxProfit(int[] prices) {

    int i;
    int j;
    int profit;
    int maxProfit = 0;
    List<Integer> profitArray = new ArrayList<Integer>();

        if (!prices.isEmpty()) {
            for (i = 0; i < prices.length; i++) {
                for (j = (i+1); j < ((prices.length)-1); j++) {
                    int diff = prices[j] - prices[i];
                    profitArray.add(diff);
                }
            }



            profit = Collections.max(profitArray);

                if (profit>0) {
                    maxProfit = profit;
                }

            maxProfit = 0;

            return maxProfit;
            }
        return 0;
        }

    }
K.Doe
  • 39
  • 1
  • 9

1 Answers1

0

If you want check your prices array is not empty then use the following code if(null !=prices && prices.length != 0)

SJN
  • 377
  • 2
  • 8
  • 18