[ rss / options / help ]
post ]
[ b / iq / g / zoo ] [ e / news / lab ] [ v / nom / pol / eco / emo / 101 / shed ]
[ art / A / beat / boo / com / fat / job / lit / map / mph / poof / £$€¥ / spo / uhu / uni / x / y ] [ * | sfw | o ]
logo
technology

Return ]

Posting mode: Reply
Reply ]
Subject   (reply to 26581)
Message
File  []
close
Guinea pig cap.png
265812658126581
>> No. 26581 Anonymous
29th May 2018
Tuesday 11:34 pm
26581 spacer
I want to make a generic printArray function in Java. Do I have to make a separate function for every primitive type like this?

public static <T> void printArray(T[] arr) { System.out.println(Arrays.toString(arr)); } public static void printArray(int[] arr) { System.out.println(Arrays.toString(arr)); } public static void printArray(double[] arr) { System.out.println(Arrays.toString(arr)); } public static void printArray(float[] arr) { System.out.println(Arrays.toString(arr)); // etc

Expand all images.
>> No. 26582 Anonymous
30th May 2018
Wednesday 12:20 am
26582 spacer
How about a little something like this:

import java.util.Arrays; public class JavaGenericPrint { public class Foo { public String toString() { return "Foo!"; } } public static void main(String[] args) { new JavaGenericPrint().run(); } public void run() { int[] ia = {1, 2, 3}; String[] sa = {"A", "B", "C"}; Foo[] fa = {new Foo(),new Foo(), new Foo()}; System.out.println("Foo array:"); printArray(fa); System.out.println("int array:"); printArray(ia); System.out.println("String array:"); printArray(sa); } public void printArray(Object o) { if(o!=null && o.getClass().isArray()) { try { java.lang.reflect.Array.get(o,0); System.out.println(Arrays.toString((Object[])o)); } catch(Exception ex) { } try { java.lang.reflect.Array.getInt(o,0); System.out.println(Arrays.toString((int[])o)); } catch(Exception ex) { } } } }


You can implement the rest of the try catch blocks for the other primitive types, using the relevant getters from https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
>> No. 26584 Anonymous
30th May 2018
Wednesday 12:27 am
26584 spacer
other TODOs:

- Make it handle zero length arrays
- Make it return after each println :D
>> No. 26585 Anonymous
30th May 2018
Wednesday 1:44 am
26585 spacer
>>26582

A little bit of sick just came up in my mouth.
>> No. 26586 Anonymous
30th May 2018
Wednesday 2:04 am
26586 spacer
This is working, but dear god there has to be a better way.

public static void printArray(Object arr) { try {System.out.println(Arrays.toString((Object[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((boolean[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((byte[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((short[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((int[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((float[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((double[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((long[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((float[]) arr));return;} catch (Exception ex) {} try {System.out.println(Arrays.toString((char[]) arr));return;} catch (Exception ex) {} System.out.println("[]"); }

>> No. 26587 Anonymous
30th May 2018
Wednesday 2:05 am
26587 spacer
>>26586

Whoops, got two floats in there.
>> No. 26588 Anonymous
30th May 2018
Wednesday 2:55 am
26588 spacer
>>26581

Praise be to Jesus for it has been many moons since I was forced to use Java but surely something like:

System.out.println(Arrays.toString((arr.getClass().getComponentType()[]) arr));

should work?

Either that or loop through the array and arr[i].toString() on each element, each class should really have its own toString() method ready for you to use.
>> No. 26589 Anonymous
30th May 2018
Wednesday 8:22 am
26589 spacer
>>26585
Yeah Java will have that effect.
>> No. 26590 Anonymous
30th May 2018
Wednesday 4:01 pm
26590 spacer
>>26581

Basically yes - but that's why java.lang.Arrays.toString() has all those same overloads.

An 'int' isn't an Object. However, an 'Integer' is - and you have easy conversion between the two types on-demand (this is called 'boxing' and the reverse 'unboxing' - https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html ).

So, if you don't want all those overloads, maybe start with an Integer[] instead (e.g. Integer[] ia = {1, 2, 3}; ) or convert from int[] to Integer[] using some crazy map func somewhere (IDK Java) before you make the call.

Also it's okay to have e.g. an ArrayList<Integer> and put ints in it rather than a plain int[]. Nowt wrong with the collections types.

... That's all I know. I use C# for a reason.
>> No. 26591 Anonymous
30th May 2018
Wednesday 4:31 pm
26591 spacer
>>26590
> I use C# for a reason.

The reason being you missed the memo that MSFT killed .NET and burried it under a mountain of unsold Windows ME CDs ?
>> No. 26592 Anonymous
30th May 2018
Wednesday 6:24 pm
26592 spacer
>>26591
Your vertices are a little too well-connected.

Return ]
whiteline

Delete Post []
Password