You are not logged in. Please login at to post your questions!


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

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"]
["lishmenta", "antidisestab"]
["cihihilipil", "lishmenta", "antidisestab"]
["ification", "nitatibus", "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);

    for (ArrayList<String> arrayList : data) {

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

asked 11 Jul '18, 22:38

phamhiepst's gravatar image

accept rate: 0%

edited 11 Jul '18, 22:39

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 11 Jul '18, 22:38

question was seen: 71 times

last updated: 11 Jul '18, 22:39