Create a list of String[], in each String[] the last char of an element must be the first char of the next element

array
string

#1

I have a String[] { “honorifi”, “cabilitudi”, “nitatibus”, “antidisestab”, “lishmenta”, “rianism”, “floccinau”, “cihihilipil”, “ification”, “supercalifrag”, “ilisticexpia” }

I expect an output is array of string[]. In each array, the last char of this element will equal to the first char of the next element. The output must be like this:

["honorifi", "ification", "nitatibus", "supercalifrag"]
["honorifi", "ilisticexpia", "antidisestab"]
["cabilitudi", "ification", "nitatibus", "supercalifrag"]
["cabilitudi", "ilisticexpia", "antidisestab"]
["nitatibus", "supercalifrag"]
["antidisestab"]
["lishmenta", "antidisestab"]
["rianism"]
["floccinau"]
["cihihilipil", "lishmenta", "antidisestab"]
["ification", "nitatibus", "supercalifrag"]
["supercalifrag"]
["ilisticexpia", "antidisestab"]

However, my code misses some cases of result.

[honorifi, ification, nitatibus, supercalifrag] [cabilitudi, ification, nitatibus, supercalifrag] [nitatibus, supercalifrag] [antidisestab] [lishmenta, antidisestab] [rianism] [floccinau] [cihihilipil, lishmenta, antidisestab] [ification, nitatibus, supercalifrag] [supercalifrag] [ilisticexpia, antidisestab]

Please review my code and help me to correct it. Thank you so much

static String[] test;

public static void main(String[] args) throws java.lang.Exception {
	// your code goes here
	test = new String[] { "honorifi", "cabilitudi", "nitatibus", "antidisestab", "lishmenta", "rianism", "floccinau", "cihihilipil", "ification", "supercalifrag", "ilisticexpia" };

	ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

	for (String string : test) {
		ArrayList<String> suite = sortSmartly(string);
		data.add(suite);
	}

	for (ArrayList<String> arrayList : data) {
		System.out.println(Arrays.toString(arrayList.toArray()));
	}
}

public static ArrayList<String> sortSmartly(String text) {
	ArrayList<String> result = new ArrayList<String>();
	result.add(text);
	String lastChar = text.substring(text.length() - 1);
	for (String object : test) {
		String firstChar = object.substring(0, 1);
		if (lastChar.equals(firstChar)) {
			result.addAll(sortSmartly(object));
			break;
		}
	}
	return result;
}