How can I initialize an array without knowing it size?

前端 未结 4 1082
青春惊慌失措
青春惊慌失措 2021-02-07 01:55

I have a situation, where I have to apply a criteria on an input array and reuturn another array as output which will have smaller size based upon the filtering criteria.

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 02:15

    Here is the code for you`r class . but this also contains lot of refactoring. Please add a for each rather than for. cheers :)

     static int isLeft(ArrayList left, ArrayList right)
    
        {
            int f = 0;
            for (int i = 0; i < left.size(); i++) {
                for (int j = 0; j < right.size(); j++)
    
                {
                    if (left.get(i).charAt(0) == right.get(j).charAt(0)) {
                        System.out.println("Grammar is left recursive");
                        f = 1;
                    }
    
                }
            }
            return f;
    
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            ArrayList left = new ArrayList();
            ArrayList right = new ArrayList();
    
    
            Scanner sc = new Scanner(System.in);
            System.out.println("enter no of prod");
            int n = sc.nextInt();
            for (int i = 0; i < n; i++) {
                System.out.println("enter left prod");
                String leftText = sc.next();
                left.add(leftText);
                System.out.println("enter right prod");
                String rightText = sc.next();
                right.add(rightText);
            }
    
            System.out.println("the productions are");
            for (int i = 0; i < n; i++) {
                System.out.println(left.get(i) + "->" + right.get(i));
            }
            int flag;
            flag = isLeft(left, right);
            if (flag == 1) {
                System.out.println("Removing left recursion");
            } else {
                System.out.println("No left recursion");
            }
    
        }
    

提交回复
热议问题