array from a seperate class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: array from a seperate class

  1. #1
    Join Date
    Sep 2005
    Posts
    1

    array from a seperate class

    Hi,

    I want to know how to create an array in a seperate class and then print the contents of an array from an another class. When I try to do this I just get a garbage value when I print the contents of the array in the second class even though I assigned a value to the array. I really need your help!!

    Thanks,
    Sunny

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by sunnny
    Hi,

    I want to know how to create an array in a seperate class and then print the contents of an array from an another class. When I try to do this I just get a garbage value when I print the contents of the array in the second class even though I assigned a value to the array. I really need your help!!

    Thanks,
    Sunny
    Something like this?
    Code:
    public class PrintArray {
        public static String printIntArray(int[] array) {
            if(array == null || array.length == 0) return "[empty]";
            StringBuffer strb = new StringBuffer("int array: ["+array[0]);
            for(int i = 1; i < array.length; i++) {
                strb.append(", "+array[i]);
            }
            strb.append("]");
            return strb.toString();
        }
    }
    And to test the class:
    Code:
    public class Test {
        public static void main(String args[]) {
            int[] array = {1,2,3,4,5};
            System.out.println(PrintArray.printIntArray(array));
        }
    }
    Good luck.
    Last edited by prometheuzz; 09-24-2005 at 04:11 AM.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I could not resist...

    ...the temptation to make this a bit more generic:

    Code:
    public class PrintArray {
      StringBuffer sb = new StringBuffer();
      public String printAnyArray(Object anArray) {
    
        if (anArray==null) return "[null]";
    
        sb.setLength(0);
        sb.append("[");
    
        if (anArray instanceof int[]) {
          int [] ii=(int[])anArray;
          if (ii.length==0) return "[empty]";
          for (int i=0; i<ii.length; i++) sb.append(ii[i]+" ");
        }
        else if (anArray instanceof float[]) {
          float [] ff=(float[])anArray;
          if (ff.length==0) return "[empty]";
          for (int i=0; i<ff.length; i++) sb.append(ff[i]+" ");
        }
        else if (anArray instanceof double[]) {
          double [] dd=(double[])anArray;
          if (dd.length==0) return "[empty]";
          for (int i=0; i<dd.length; i++) sb.append(dd[i]+" ");
        }
        else if (anArray instanceof Object[]) {
          Object [] oo=(Object[])anArray;
          if (oo.length==0)  return "[empty]";
          for (int i=0; i<oo.length; i++) sb.append(oo[i]+" ");
        }
    
        sb.append("]");
        return sb.toString();
      }
    
      public static void main(String args[]) {
        PrintArray pA=new PrintArray();
        int[] iArray = {
            1, 2, 3, 4, 5};
        double [] dArray = {
          1.4,1.5,1.6
        };
        String [] strArray= {
            "One","Two","Three"
        };
        CustClass [] ccArray= {
            new CustClass(11),
            new CustClass(22),
            new CustClass(33)
        };
        System.out.println(pA.printAnyArray(iArray));
        System.out.println(pA.printAnyArray(dArray));
        System.out.println(pA.printAnyArray(strArray));
        System.out.println(pA.printAnyArray(ccArray));
      }
    }
    class CustClass  {
      int val=Integer.MIN_VALUE;
      public CustClass(int val) {
        this.val=val;
      }
      public String toString() {
        return "*"+val+"*";
      }
    }
    eschew obfuscation

Similar Threads

  1. array initializer for class member
    By jab630 in forum C++
    Replies: 2
    Last Post: 09-21-2005, 08:49 AM
  2. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  3. Replies: 2
    Last Post: 03-06-2003, 08:12 AM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Object Array ? Do you Worth it
    By ashish in forum Java
    Replies: 1
    Last Post: 05-23-2002, 07:50 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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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