DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Array elements

  1. #1
    Join Date
    Oct 2006

    Array elements


    I am new here - and i signed up as i am at University studying computer science and need help with Java programming tasks.

    Any help will be appreciated. - I have a firm knowledge of basic concepts in Java.

    My task today is to build a method which will detect wether a given element is present within an array starting

    public boolean contains(Object [] a, Object x)

    and it will return true if and only if there is an element y in such that x.equals(y) returns true.

    Can you guys with me a bit of help with this.

    Thanks very much - Andy
    I learn best via example - which is why i have posted the question.

  2. #2
    Join Date
    Oct 2006

    Answer to your Question

    here u go:
    public class TestObj {
      public static void main (String [] args)throws Exception{
    	  a ob0 = new a("a","b");
    	  a ob1 = new a("c","d");
    	  a ob2 = new a("e","f");
    	  a ob3 = new a("g","h");
    	  a ob4 = new a("i","j");
    	  a ob5 = new a("k","l");
    	  a ob6 = new a("m","n");
    	  a ob7 = new a("d","f");
    	  Object [] objArray = {ob0, ob1,ob2,ob3,ob4,ob5,ob6,ob7};
    	 public static boolean contains(Object [] a, Object x) {
    		 boolean rvalue = false;
    		 for(int i = 0; i < a.length ; i++){
    				 rvalue = true;
    		 return rvalue;
    But remember you might have to override the equal methods for your objects to work properly.....i.e. how are the two objects Comparable, on what attributes ????

    class a{
    	String fname="bla";
    	String lname="bloo";
    	public a(){
    		this.fname = "";
    	    this.lname = "";
    	public a(String fname, String lname){
    		this.fname = fname;
    		this.lname = lname;
    	public boolean equals(Object obj){
    		if(this.fname.equals(((a)obj).fname) && this.lname.equals(((a)obj).lname) )
    				return true;
    		else return false;

    good luck
    Last edited by dreamz; 10-02-2006 at 09:59 PM. Reason: To complete the code

Similar Threads

  1. Replies: 4
    Last Post: 05-31-2006, 12:11 PM
  2. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  4. accessing textfield elements in a array
    By dominic in forum Java
    Replies: 2
    Last Post: 04-30-2001, 02:27 AM
  5. Replies: 3
    Last Post: 01-11-2001, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.