자바 7 153 141 114 바이트
String r="";<T,S>S c(S s,T o){for(T x:(T[])o)if(x instanceof Object[])c("-"+s,x);else r+=s+">"+x+"\n";return(S)r;}
@ Barteks2x 덕분에 -39 바이트
설명:
String r=""; // Result String outside the method / on class-level
<T,S> S c(S s, T o){ // Recursive Method with generic String and Object parameters and String return-type
for(T x : (T[])o) // Loop over the input-array
if(x instanceof Object[]) // If the current item is an array itself:
c("-"+s, x); // Recursive method-call with this array
else // Else:
r += s+">"+x+"\n"; // Append return-String with stripes String-input, ">", current item, and a new-line
// End of loop (implicit / single-line body)
return (S)r; // Return the result-String
} // End of method
테스트 코드 :
여기에서 시도하십시오.
class M{
String r="";<T,S>S c(S s,T o){for(T x:(T[])o)if(x instanceof Object[])c("-"+s,x);else r+=s+">"+x+"\n";return(S)r;}
public static void main(String[] a){
M m = new M();
System.out.println(m.c("", new Object[]{new Object[]{1,2},new Object[]{new Object[]{1,2},3},4,new Object[]{new Object[]{new Object[]{new Object[]{5}}}},6}));
m.r = "";
System.out.println(m.c("", new Object[]{"Atom",new Object[]{"Proton",new Object[]{"Up Quark","Up Quark","Down Quark"}},new Object[]{"Neutron",new Object[]{"Up Quark","Up Quark","Down Quark"}},"Electron"}));
}
}
산출:
->1
->2
-->1
-->2
->3
>4
---->5
>6
>Atom
->Proton
-->Up Quark
-->Up Quark
-->Down Quark
->Neutron
-->Up Quark
-->Up Quark
-->Down Quark
>Electron