Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would recommend to adapt the <code>String[][]</code> to a <code>CharSequence</code>. Then you are free to do everything you can do with a <code>CharSequence</code> and this also means that you can use <code>java.util.regex.Matcher</code> to search for the string and you don't need to implement an own search algorithm. </p> <p>For example:</p> <pre><code>public class Main { public static void main(String[] args) { String[][] array2d = createArray(); int charSeqColumn = 0; CharSequence charSequnce = new Array2DColumnCharSequnce(array2d, charSeqColumn); System.out.println(charSequnce.toString()); Pattern patttern = Pattern.compile("ext"); Matcher matcher = patttern.matcher(charSequnce); while (matcher.find()) { String matchGroup = matcher.group(); int start = matcher.start(); int end = matcher.end() - 1; String msg = MessageFormat.format("{0} matched at: [{1}] - [{2}]", matchGroup, start, end); System.out.println(msg); } } private static String[][] createArray() { String[][] array2d = new String[2][10]; array2d[0][0] = "N"; array2d[0][1] = "e"; array2d[0][2] = "x"; array2d[0][3] = "t"; array2d[0][4] = " "; array2d[0][5] = "N"; array2d[0][6] = "e"; array2d[0][7] = "x"; array2d[0][8] = "t"; array2d[0][9] = " "; array2d[1][0] = "H"; array2d[1][1] = "e"; array2d[1][2] = "l"; array2d[1][3] = "l"; array2d[1][4] = "o"; array2d[1][5] = "W"; array2d[1][6] = "o"; array2d[1][7] = "r"; array2d[1][8] = "l"; array2d[1][9] = "d"; return array2d; } } </code></pre> <p>will output</p> <pre><code>Next Next ext matched at: [1] - [3] ext matched at: [6] - [8] </code></pre> <p>I would implement the <code>CharSequence</code> adaption like this</p> <pre><code>class Array2DColumnCharSequnce implements CharSequence { private int column; private String[][] array2d; private int endIndex; private int startIndex; public Array2DColumnCharSequnce(String[][] array2d, int column) { this(array2d, column, 0, array2d[column].length); this.array2d = array2d; this.column = column; } public Array2DColumnCharSequnce(String[][] array2d, int column, int startIndex, int endIndex) { this.array2d = array2d; this.column = column; this.startIndex = startIndex; this.endIndex = endIndex; } public int length() { return endIndex - startIndex; } public char charAt(int index) { String charString = array2d[column][startIndex + index]; return charString.charAt(0); } public CharSequence subSequence(int start, int end) { Array2DColumnCharSequnce array2dColumnCharSequnce = new Array2DColumnCharSequnce( array2d, column, start, end); return array2dColumnCharSequnce; } @Override public String toString() { StringBuilder sb = new StringBuilder(this); return sb.toString(); } } </code></pre> <p><strong>Note</strong>: The <code>Array2DColumnCharSequnce</code> is just a quick implementation and it does not address exception handling yet nor it addresses what happens when there are more than one char in a string column.</p> <p><strong>Why to use a <code>CharSequence</code> decorator</strong></p> <p>The difference with adapting the array to a <code>CharSequence</code> to other approaches is that you use a standard java interface that can be re-used with many other classes and thus is very flexible.</p> <p>Some often used standard java classes that take a <code>CharSequence</code> as parameter</p> <ul> <li><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence-" rel="nofollow"><code>String.contains(CharSequence s)</code></a></li> <li><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contentEquals-java.lang.CharSequence-" rel="nofollow"><code>String.contentEquals(CharSequence cs)</code></a></li> <li><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-" rel="nofollow"><code>String.replace(CharSequence target, CharSequence replacement)</code></a></li> <li><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Appendable.html#append-java.lang.CharSequence-" rel="nofollow"><code>Appendable.append(CharSequence csq)</code></a></li> <li><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html#append-java.lang.CharSequence-" rel="nofollow"><code>StringBuffer.append(CharSequence s)</code></a></li> <li><a href="http://docs.oracle.com/javase/8/docs/api/index.html?java/lang/CharSequence.html" rel="nofollow"><code>StringBuilder.append(CharSequence s)</code></a></li> </ul> <p>See the full list <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/class-use/CharSequence.html" rel="nofollow">here</a>.</p> <p>Use the code above and try this to see how flexibe the decorator is.</p> <pre><code>public static void main(String[] args) { String[][] array2d = createArray(); CharSequence charSequnce = new Array2DColumnCharSequnce(array2d, 0); boolean contentEquals = "Next Next ".contentEquals(charSequnce); System.out.println(contentEquals); CharSequence column1CharSequnce = new Array2DColumnCharSequnce(array2d, 1); String replaced = "I want to say Next Next ".replace(charSequnce, column1CharSequnce); System.out.println(replaced); } </code></pre> <p>will output</p> <pre><code>true I want to say HelloWorld </code></pre> <p>Finally everyone has to decide what he/she wants and what fits the situation. I prefer implementations that give me more options if I can get them "almost" for free.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload