这个问题,其实和提取字符串没有任何关系。
我写了个C#的例子,你自己把它翻译成Java的吧。算法是通用的。
[CODE]//---------------------------------------------------------
// <copyright file="Program.cs" company="Yaping Xin">
// Copyright (c) Yaping Xin. All rights reserved.
// </copyright>
// <Description>Demo.</Description>
//---------------------------------------------------------
namespace Demo100816A01
{
using System;
using System.Text;
/// <summary>
/// Application entry class.
/// </summary>
internal class Program
{
/// <summary>Const definition of how many number as a loop</summary>
private const int LoopCount = 3;
/// <summary>
/// Application entry point.
/// </summary>
/// <param name="args">Command line parameters</param>
internal static void Main(string[] args)
{
Do(new int[] { 1, 2, 3, 4, 5 });
Do("12345");
Do("abcde");
}
/// <summary>
/// Rearrange the int array and print out result
/// </summary>
/// <param name="src">the int array</param>
private static void Do(string src)
{
string seprator = "";
Do<char>(src.ToCharArray(), seprator);
}
/// <summary>
/// Rearrange the int array and print out result
/// </summary>
/// <param name="src">the int array</param>
private static void Do(int[] src)
{
string seprator = ", ";
Do<int>(src, seprator);
}
/// <summary>
/// Rearrange the array and print out result
/// </summary>
/// <typeparam name="T">Element data type of the array</typeparam>
/// <param name="src">the array to be print</param>
/// <param name="seprator">seprator between each element in print string</param>
private static void Do<T>(T[] src, string seprator)
{
int[] dimensions = RearrangeDimension(src.Length);
string message = string.Format(
"Source: {0}\tResult: {1}",
PrintStringOfArray<T>(src, seprator),
PrintStringOfRearrangedArray<T>(src, dimensions, seprator));
System.Console.WriteLine(message);
}
/// <summary>
/// Rearrange the dimension of the array.
/// </summary>
/// <param name="capacity">capacity of the array</param>
/// <returns>Rearranged dimension</returns>
private static int[] RearrangeDimension(int capacity)
{
if (capacity < 1)
{
throw new ArgumentException("capacity should at least be greater than 0.");
}
if (capacity == 1)
{
return new int[1] { 0 };
}
bool[] src = new bool[capacity];
for (int i = 0; i < capacity; i++)
{
src[i] = true;
}
int[] dimensions = new int[capacity];
int currentCapacity = 0;
int position = -1;
int count = 0;
while (currentCapacity < capacity)
{
while (count < LoopCount)
{
position = position < capacity - 1 ? ++position : 0;
if (src[position])
{
count++;
}
}
count = 0;
src[position] = false;
dimensions[currentCapacity++] = position;
}
return dimensions;
}
/// <summary>
/// Gets the print string of array
/// </summary>
/// <typeparam name="T">Element data type of the array</typeparam>
/// <param name="src">the array to be print</param>
/// <param name="seprator">seprator between each element in print string</param>
/// <returns>the print string</returns>
private static string PrintStringOfArray<T>(T[] src, string seprator)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < src.Length; i++)
{
sb.Append(src[i].ToString());
if (i < src.Length - 1)
{
sb.Append(seprator);
}
}
return string.Format("[{0}]", sb.ToString());
}
/// <summary>
/// Gets the print string of rearranged array
/// </summary>
/// <typeparam name="T">Element data type of the array</typeparam>
/// <param name="src">the array to be rearranged</param>
/// <param name="dimensions">rearranged dimensions</param>
/// <param name="seprator">seprator between each element in print string</param>
/// <returns>the print string</returns>
private static string PrintStringOfRearrangedArray<T>(T[] src, int[] dimensions, string seprator)
{
StringBuilder sb = new StringBuilder();
int position = 0;
foreach (int dimension in dimensions)
{
sb.Append(src[dimension].ToString());
position++;
if (position < src.Length)
{
sb.Append(seprator);
}
}
return string.Format("[{0}]", sb.ToString());
}
}
}
[/CODE]
运行结果:
[IMG=0,absmiddle]http://xinsoft.org/pub/dailyreading/2010/0816/result-Demo100816A01.jpg[/IMG]