23

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.

Now problem is I do not know the size of filtered results, so I can not initialize the array with specific value. And I do not want it to be large size will null values because I am using array.length; later on.

One way is to first loop the original input array and set a counter, and then make another loop with that counter length and initialize and fill this array[]. But is there anyway to do the job in just one loop?

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
Space Rocker
  • 787
  • 3
  • 11
  • 25

5 Answers5

39

You can't... an array's size is always fixed in Java. Typically instead of using an array, you'd use an implementation of List<T> here - usually ArrayList<T>, but with plenty of other alternatives available.

You can create an array from the list as a final step, of course - or just change the signature of the method to return a List<T> to start with.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • why would you prefer `ArrayList` to `LinkedList` in this situation? – Roman Dec 26 '10 at 19:02
  • @Roman: I just naturally reach for an ArrayList in general. LinkedList would be fine too... it's a more expensive in terms of memory, of course, but it doesn't require copying elements on expansion. – Jon Skeet Dec 26 '10 at 19:15
  • @Roman: See [When to use LinkedList over ArrayList](http://stackoverflow.com/q/322715/18192) for discussion. That being said, your first inclination should be `ArrayList`. – Brian Dec 27 '10 at 15:33
3

Use LinkedList instead. Than, you can create an array if necessary.

Roman
  • 64,384
  • 92
  • 238
  • 332
  • 1
    `ArrayList` would probably be more appropriate – Noel M Dec 26 '10 at 19:00
  • 1
    @Noel M: why? I think it wouldn't. We don't know the number of elements. So, with LinkedList every add (i.e. addLast) operation works in O(1) and do really a little job, while `ArrayList` will autoincrease its size several times and these're costly operations. – Roman Dec 26 '10 at 19:04
  • 1
    On the other hand, with a LinkedList you're creating a Node object for each element. You claim that expansion is a "costly" operation - it's only a matter of creating a new array and copying the existing elements (which can be a fast array copy). I don't think it's simple to say which is uniformly "better" for this situation. – Jon Skeet Dec 26 '10 at 19:16
3

Just return any kind of list. ArrayList will be fine, its not static.

    ArrayList<yourClass> list = new ArrayList<yourClass>();
for (yourClass item : yourArray) 
{
   list.add(item); 
}
stbn
  • 425
  • 5
  • 4
0

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<String> left, ArrayList<String> 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<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();


        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");
        }

    }
User123456
  • 2,492
  • 3
  • 30
  • 44
0

If you are using Java 8 or later, you can use Stream. This is an example of extracting only even numbers in int[].

static int[] evenFilter(int[] input) {
    return IntStream.of(input)
        .filter(i -> i % 2 == 0)
        .toArray();
}

public static void main(String args[]) throws IOException {
    int[] input = {3, 4, 22, 36, 49, 51};
    int[] output = evenFilter(input);
    System.out.println(Arrays.toString(output));
}

output:

[4, 22, 36]