Return an array of Pairs which have the same length as the input array of Strings. (Java) -
i want create class return array of pairs have same length input array of strings. in addition, pair should have first letter of string , length of string.
for example; create(new string[] {"clue", "yay", "neon", "halala"}) should return array of pairs {[’c’,4],[’y’,3],[’n’,4],['h',6]}
so, input , output both arrays. output has in form of pair. here's tried:
import java.util.arrays; public class couple { public static couple[] create(string[] source){ (int = 0; < source.length; i++) { system.out.print("["+","+source.length+"]") ; } return null; } public static void main(string[] args) { system.out.println(arrays.tostring(create(new string[] {"clue", "yay", "neon", "halala"}))); } }
as it's obvious there few errors+ dont want return null. sake of testing code, had it. ideas?
public class couple { private char firstchar; private int length; public couple(char firstchar, int length) { this.length = length; this.firstchar = firstchar; } public static couple[] create(string[] source) { couple[] couples = new couple[source.length]; // create array hold return pairs (int = 0; < source.length; i++) { string entry = source[i]; if (entry != null) { couples[i] = new couple(entry.charat(0), entry.length()); } else { // want if there's null value? // until answer we'll leave corresponding couple null aswell } } return couples; } @override public string tostring() { return "couple{" + "firstchar=" + firstchar + ", length=" + length + '}'; } }
Comments
Post a Comment