Java 检查数组中的字符以生成回文列表

Java 检查数组中的字符以生成回文列表,java,arrays,palindrome,Java,Arrays,Palindrome,我需要做一个数组,用25个5位数的回文数字填充它。我为以前的赋值做了一个方法,使用for循环反向输出它。对于这一个,我想在数组中完成这一切,学习一些新的东西。我发现我可以通过添加两个字符来生成00000-99999,并将所有范围设置为0-9。完整代码见文章底部 为了澄清,main()中使用了digit1,如下所示: char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; char[] digit2 = {'0', '

我需要做一个数组,用25个5位数的回文数字填充它。我为以前的赋值做了一个方法,使用for循环反向输出它。对于这一个,我想在数组中完成这一切,学习一些新的东西。我发现我可以通过添加两个字符来生成00000-99999,并将所有范围设置为0-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];
我尝试将PrintIndexes方法的输出部分转换为字符串,认为可以使用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)以外的任何值,我都会得到以下错误:

如果我使用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));
 }

}
公共类回文数组
{
静态字符[]数字1={'0','1','2','3','4','5','6','7','8','9'};
静态字符[]数字2={'0','1','2','3','4','5','6','7','8','9'};
静态字符[]数字3={'0','1','2','3','4','5','6','7','8','9'};
静态字符[]数字4={'0','1','2','3','4','5','6','7','8','9'};
静态字符[]数字5={'0','1','2','3','4','5','6','7','8','9'};
静态布尔递增索引(char[]arrs,int[]inds)
{
int n=阵列长度;
对于(int i=n-1;i>=0;i--)
{
if(inds[i]0;a++)
{
相反+=短字符(a);
}
返回相反的。等于(str);
}
公共静态void main(字符串[]args)
{
char[]arrs={digit1,digit2,digit3,digit4,digit5};
int[]inds=新的int[5];
做
{
打印索引(ARR、IND);
} 
while(递增指数(arrs,inds));
}
}

您始终可以创建一个方法,如

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);
                        }
                    }
                }
            }
        }
    }

我最终完全抛弃了代码,走了另一条路。借用了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();
}
}
import java.util.Random;
公共类回文数组
{
静态void arrayFill()
{
整数倍=0,赔率=0;
int[]回文=新int[25];
int j=0;
for(int i=10001;i<100000;i++)//从第一个5位回文开始计算
{
字符串s1=整数。toString(i);
StringBuilder sb=new StringBuilder(s1);//字符串使我们能够使用sb.reverse进行快速轻松的检查
如果(s1.等于(sb.reverse().toString()))
{
Random r=new Random();//PRNG这样就不会每次都出现相同的数字
i=r.nextInt((99999-10001)+1)+10001;//确保生成的种子在所需范围内
回文[j++]=i;
如果(j==25)
{
打破
}
}
}
对于(int i=0;i<25;i++)
{
if(回文[i]%2==0)
{
System.out.println(“下标“+i+”=“+palindrome[i]+”-->偶数”)处的回文;
evens++;
}
其他的
{
System.out.println(“下标“+i+”=“+palindrome[i]+”-->奇数处的回文”);
赔率++;
}
}
System.out.println(“\n偶数回文的总数为”+evens);
System.out.println(“\n奇数回文的总数为”+赔率);
}
公共静态void main(字符串[]args)
{
数组填充();
}
}
/*
项目名称:回文实验室
程序员:杰克
课程:计算机科学
日期:2016年10月14日
描述:简单的回文程序
*/
导入java.util.Scanner;
公共类新回文实验室
{ 
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎使用回文转换器”);
System.out.print(“输入您想要的回文==>”;
字符串palin=input.nextLine();
println(“原版是:“+palin”);
palin=palin.toLowerCase();
int length=palin.length();
char[]tempcharray=新字符[长度];
char[]charArray=新字符[长度];

对于(int i=0;i如果在尝试使用
charAt(1)访问第二个字符之前打印
output
,您会得到什么
?读了你的问题,我有点不知所措…无法理解你的问题,你定义了
digit1
数组,但从未使用过,谁知道什么是
arrs,inds…
你的最终目标是什么?生成一个数组,其中有25个5位回文数字?哪25个数字重要?为digit1…部分添加了说明。@olavimutanoja,我得到“0”,然后是pr
    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);
                        }
                    }
                }
            }
        }
    }
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();
}
}
/*
Project Name: PalindromeLab
Programmer: Jack
Class: Computer Science
Date: 10/14/16
Description: Simple Palindrome Programm
*/
import java.util.Scanner;

public class NewPalindromeLab
{ 
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.println("Welcome to Palindrome Converter");
      System.out.print("Enter in what you want Palindromeinated ==> ");
      String palin = input.nextLine();
      System.out.println("The origional was: " + palin);

      palin = palin.toLowerCase();

      int length = palin.length();

      char[] tempCharArray = new char[length];
      char[] charArray = new char[length];

      for(int i=0; i<length; i++)
      {
         tempCharArray[i] = palin.charAt(i);
      } 

      for(int k=0; k<length; k++)
      {
         charArray[k] = tempCharArray[length - 1 - k];
      }

      String reversePalin = new String (charArray);
      String reverseNewPalin = "";
      reverseNewPalin = reversePalin.replaceAll("[^A-Za-z0-9]", "");

      System.out.println("The reversed word is: " + reverseNewPalin);

      if (reversePalin.equals(palin))
      {
         System.out.println("This is a palindrome!");
      }
      else      
      {
         System.out.println("This is not a palindrome!");
      }
   }
}

/*
The following is the Output of the program:
Welcome to Palindrome Converter
Enter in what you want Palindromeinated ==>  Hello
The origional was:  Hello
The reversed word is: olleh
This is not a palindrome!
*/