4 Replies - 8655 Views - Last Post: 26 September 2009 - 01:31 PM Rate Topic: -----

#1 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

method to find number of elements within an array

Post icon  Posted 25 September 2009 - 08:49 PM

I want to search and array to find if a particualr name exist within the array but I dont want to search every element within the array only filled elements. Is there a way to search an array to find out if the element contains data and to only search the contents of that array element if it does?I don't know if this is understandable?

This is what I have so far I need to search for a persons name but I don't want to search through all 500 arrays of the ExtPerson type. I nned to write a method that searches for a persons name and returns true or false if they exist within the AddressBook.

import java.util.*;
import java.io.*;

public class AddressBook
{
	private ExtPerson[] list;
	private int numberOfElements;
	
	public AddressBook()
	{
		list = new ExtPerson[500];
		for(int index = 0; index < 500; index++)
		{
			list[index] = new ExtPerson();
		}
		numberOfElements = 0;
	}
	
	public void printArray(ExtPerson[] array)
	{
		int index;
		
		for(index = 0; index < array.length; index++)
			System.out.println(array[index]);
	}
	public boolean findPerson(String first, String last)
	{
		for(int index = 0; index < numberOfElements; index++);
		{
			
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: method to find number of elements within an array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: method to find number of elements within an array

Posted 25 September 2009 - 08:53 PM

no need to fill your array with "empty" new ExtPerson()
just create a new ExtPerson() when you need to add one and use numberOfElements to know where to add and where to stop scanning
Was This Post Helpful? 0
  • +
  • -

#3 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

Re: method to find number of elements within an array

Posted 25 September 2009 - 09:13 PM

how would i go about incrementing the number of elements whenever there is a new ExtPerson object that has been instantiated?



View Posthidden_within, on 25 Sep, 2009 - 07:49 PM, said:

I want to search and array to find if a particualr name exist within the array but I dont want to search every element within the array only filled elements. Is there a way to search an array to find out if the element contains data and to only search the contents of that array element if it does?I don't know if this is understandable?

This is what I have so far I need to search for a persons name but I don't want to search through all 500 arrays of the ExtPerson type. I nned to write a method that searches for a persons name and returns true or false if they exist within the AddressBook.

import java.util.*;
import java.io.*;

public class AddressBook
{
	private ExtPerson[] list;
	private int numberOfElements;
	
	public AddressBook()
	{
		list = new ExtPerson[500];
		for(int index = 0; index < 500; index++)
		{
			list[index] = new ExtPerson();
		}
		numberOfElements = 0;
	}
	
	public void printArray(ExtPerson[] array)
	{
		int index;
		
		for(index = 0; index < array.length; index++)
			System.out.println(array[index]);
	}
	public boolean findPerson(String first, String last)
	{
		for(int index = 0; index < numberOfElements; index++);
		{
			
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: method to find number of elements within an array

Posted 25 September 2009 - 09:18 PM

View Posthidden_within, on 25 Sep, 2009 - 08:13 PM, said:

how would i go about incrementing the number of elements whenever there is a new ExtPerson object that has been instantiated?

list[numberOfElements++] = new ExtPerson(.....

so the first time it will store the new ExtPerson() into list[0] and increment numberOfElements to 1
so the second time it will store the new ExtPerson() into list[1] and increment numberOfElements to 2
...
Was This Post Helpful? 0
  • +
  • -

#5 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

Re: method to find number of elements within an array

Posted 26 September 2009 - 01:31 PM

View Postpbl, on 25 Sep, 2009 - 08:18 PM, said:

View Posthidden_within, on 25 Sep, 2009 - 08:13 PM, said:

how would i go about incrementing the number of elements whenever there is a new ExtPerson object that has been instantiated?

list[numberOfElements++] = new ExtPerson(.....

so the first time it will store the new ExtPerson() into list[0] and increment numberOfElements to 1
so the second time it will store the new ExtPerson() into list[1] and increment numberOfElements to 2
...


Thanks alot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1