Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Finding a specific object inside an array of objects

  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Finding a specific object inside an array of objects

    Suppose you have a code fragment that counts the # of occurrences of a specific “target” integer in an array.

    Java Code:
    	int i;
    	int answer = 0;
    
    	for (i = 0; i < data.length; i++)
    	if (data[i] == target)
    	   answer++;
    Let's say I want the data array to be an array of objects and target is a non-null reference to an object with an equals method. Is this an effective way to rewrite the code presented above?

    Java Code:
         int i;
    	int answer = 0;
    
    	for (i = 0; i < data.length; i++)
    	if (target.equals(data[i]))
    	   answer++

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Finding a specific object inside an array of objects

    // assume that target is an instance of Integer.

    Java Code:
    int count = 0;
    for ( Integer element : data) {
       if (target.equals(element)) {
            count++;
       }
    }
    Notice, that the variable count is more descriptive of what you are doing and that I included the braces. Even though your method is syntactically correct I would recommend to always use braces. It can ultimately save you time debugging the code.

    You can also do the following in Java 8.

    Java Code:
    long count = Stream.of(data).filter(e -> e.equals(target)).count();
    Regards,
    Jim
    Last edited by jim829; 10-25-2015 at 01:18 AM.
    csanch11 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Finding a specific object inside an array of objects

    After some thought, I believe this version of the Java 8 solution might be better.

    Java Code:
    long count = Stream.of(data).filter(e -> target.equals(e)).count();
    The advantage is that it allows for nulls in the data array without throwing an exception. The disadvantage is
    that target must be a non-null Integer instance.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 10-15-2012, 01:47 PM
  2. Multiple object instances in array of objects
    By Drizzt in forum New To Java
    Replies: 6
    Last Post: 01-12-2012, 04:07 PM
  3. Filereader finding specific characters
    By busdude in forum New To Java
    Replies: 2
    Last Post: 09-29-2011, 02:04 PM
  4. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  5. finding Operating System specific data
    By FrankyDee in forum New To Java
    Replies: 2
    Last Post: 09-28-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •