-
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
-
 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.
-
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
-
Replies: 2
Last Post: 09-21-2005, 08:49 AM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 2
Last Post: 03-06-2003, 08:12 AM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks