检查数组中的字符以构建回文列表

时间:2014-11-26 21:39:18

标签: java arrays palindrome

我需要制作一个数组,并用25个5位数的回文数字填充它。我为之前的赋值创建了一个方法,该方法使用for循环反向输出。对于这个,我想在阵列中完成所有这些,以学习新的东西。我发现this code我可以通过添加两个字符来生成00000 - 99999,并使所有范围都为0 - 9. 请参阅帖子的底部以获取完整代码。

char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

为了澄清,在main()中使用了digit1,如下所示:

char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit2 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit3 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit4 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit5 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

char[][] arrs = {digit1, digit2, digit3, digit4, digit5};
int[] inds = new int[5];

我尝试将printIndices方法的输出部分转换为String,以为我可以使用charAt或StringBuilder.reverse()。现在我有这个:

String output = String.valueOf(arrs[i][inds[i]]);
System.out.print(output);

哪种方法可行,但如果我尝试这样做:

String palindrome = new StringBuilder(output).reverse().toString();
System.out.print(palindrome); 

我得到与以前完全相同的字符 - 我是否正确认为如果使用该方法反转00001,结果将是10000?

如果我使用charAt:

char output1 = output.charAt(1);
System.out.print(output1);

除了charAt(0)之外的任何值,我都会收到以下错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:646)
at PalindromeArray.printIndices(PalindromeArray.java:31)
at PalindromeArray.main(PalindromeArray.java:51)
Java Result: 1

如果我使用charAt(0),它会打印所有数字,这使我认为它们不被视为可以分开的单个字符,因此如果我尝试访问过去的任何内容时会出错第一个。

public class PalindromeArray

{
    static char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit2 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit3 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit4 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit5 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

static boolean incrementIndices(char[][] arrs, int[] inds) 
{
int n = arrs.length;
for(int i = n - 1; i >= 0; i--) 
{
    if(inds[i] < arrs[i].length - 1) 
    {
        inds[i]++;
        return true;
    }
    inds[i] = 0;
}
return false; // could not increment further
}

static void printIndices(char[][] arrs, int[] inds) 
{
int n = arrs.length;
for(int i = 0; i < n; i++) 
{

    String output = String.valueOf(arrs[i][inds[i]]);

    for(char c1: digit1)
{
    for(char c2: digit1)
    {
        for(char c3: digit1)
        {
            for(char c4: digit1)
            {
                for(char c5: digit1)
                {
                    String tempCheck = ""+c1+c2+c3+c4+c5;
                    if(isPalindrome(tempCheck))
                    {
                        System.out.println(tempCheck);
                    }
                }
            }
        }
    }
}

    System.out.print(output);


}
System.out.println();
}

public static boolean isPalindrome(String str)
{
String opposite = "";
for(int a = str.length()-1; a > 0; a++)
{
    opposite += str.charAt(a);
}
return opposite.equals(str); 
}

public static void main(String[] args)
{


    char[][] arrs = {digit1, digit2, digit3, digit4, digit5};
    int[] inds = new int[5];

    do 
    {
        printIndices(arrs, inds);
    } 
    while(incrementIndices(arrs, inds));
 }

}

3 个答案:

答案 0 :(得分:1)

SELECT
  o.orderdate
FROM Sales.Orders o
WHERE (o.orderdate < '2007-01-01' AND o.orderdate < '2007-03-01')
  OR (o.orderdate < '2008-01-01' AND o.orderdate < '2008-03-01')
 ORDER BY o.orderdate;

答案 1 :(得分:0)

您始终可以创建一个类似

的方法
public static boolean isPalindrome(String str)
{
    String opposite = "";
    for(int a = str.length()-1;a>0;a++)
    {
        opposite+=str.charAt(a);
    }
    return opposite.equals(str);
}

编辑:我将编写实现它的方法。

    for(char c1: digit1)
    {
        for(char c2: digit1)
        {
            for(char c3: digit1)
            {
                for(char c4: digit1)
                {
                    for(char c5: digit1)
                    {
                        String tempCheck = ""+c1+c2+c3+c4+c5;
                        if(isPalindrome(tempCheck))
                        {
                            System.out.println(tempCheck);
                        }
                    }
                }
            }
        }
    }

答案 2 :(得分:0)

我最终完全抛弃了代码并走了另一条路。借用Java论坛中的某个人进行同样的练习,使用随机数生成器对其进行改进,使得填充的整数范围对项目有意义,并添加计数器以总计回文数。

感谢所有贡献者 - 我正在学习很多关于能做什么和不能做什么的事。

import java.util.Random;
public class PalindromeArray
{
static void arrayFill()
{
  int evens = 0, odds = 0;
  int []palindrome = new int [25];
  int j = 0;
  for(int i = 10001; i < 100000; i++) //Start our counter at the first 5-digit palindrome
  {
    String s1 = Integer.toString(i);
    StringBuilder sb = new StringBuilder(s1); //Strings enables us to use sb.reverse for a quick and easy check
    if(s1.equals(sb.reverse().toString()))
    {
        Random r = new Random(); //PRNG so the same numbers don't come up every time
        i = r.nextInt((99999 - 10001) + 1) + 10001; //Ensure generated seed is in desired range
        palindrome[j++] = i;
        if(j == 25)
        {
            break;
        }
    }
  }

  for(int i = 0; i < 25; i++)
  {
    if(palindrome[i]%2 == 0)
    {
        System.out.println("The palindrome at subscript " + i + " = " + palindrome[i] + " --> even");
        evens++;
    }
    else
    {
        System.out.println("The palindrome at subscript " + i + " = " + palindrome[i] + " --> odd");
        odds++;
    }
  }

  System.out.println("\nThe total number of even palindromes is " + evens);
  System.out.println("\nThe total number of odd palindromes is " + odds);
}

public static void main(String[] args)
{
   arrayFill();
}
}