如何在java中使用数组反转数字?
我正在编写一个代码来反转用户使用java输入的数字。为此,我使用了一个数组。我面临的问题是在调用main函数中的方法时返回数组如何在java中使用数组反转数字?,java,Java,我正在编写一个代码来反转用户使用java输入的数字。为此,我使用了一个数组。我面临的问题是在调用main函数中的方法时返回数组 我知道您想要反转数组。。。因此,您可以使用 ArrayUtils.reverse(int[] array) 例如,您可以执行以下操作: public static void main(String[] arg){ int[] arr = { 1,2,3,4,5,6}; ArrayUtils.reverse(arr); System.out.pr
我知道您想要反转数组。。。因此,您可以使用
ArrayUtils.reverse(int[] array)
例如,您可以执行以下操作:
public static void main(String[] arg){
int[] arr = { 1,2,3,4,5,6};
ArrayUtils.reverse(arr);
System.out.println(Arrays.toString(arr));
// Prints: [6, 5, 4, 3, 2, 1]
}
但是,如果您想自己编写反向代码,如果您查看ArrayUtils.reverse的源代码,您可以找到Apache人员是如何做到这一点的。以下是实施情况:
public static void reverse(int[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
int tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public class StackForStringReverse {
public static void main(String[] args) {
/*create a new string */
StringBuffer s= new StringBuffer("GeeksQuiz");
/*call reverse method*/
reverse(s);
/*print the reversed string*/
System.out.println("Reversed string is " + s);
}
private static void reverse(StringBuffer str) {
int n=str.length();
MyStack obj=new MyStack(n);
// Push all characters of string
// to stack
int i;
for (i = 0; i < n; i++)
obj.push(str.charAt(i));
// Pop all characters of string and put them back to str
for (i = 0; i < n; i++)
{
char ch = obj.pop();
str.setCharAt(i,ch);
}
}
}
class MyStack{
int size;
char[] a;
int top;
public MyStack(int n) {
top=-1;
size=n;
a=new char[size];//{'1','f','f','f'};
}
public boolean isEmpty() {
return top<0;
}
/**
* Insert data into Stack if it's not full*/
public void push(char c) throws IllegalArgumentException{
if(top>=size) {
throw new IllegalArgumentException("Stack overflow.");
}
a[++top]=c;
}
public char pop() {
if(top<0) {
throw new IllegalArgumentException("Stack underflow.");
}
char x=a[top--];
return x;
}
public void clear() {
while(top>0) {
top--;
size=top;
}
}
/**
* Display the data inserted */
public void display() {
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.println("");
}
}
可以使用StringBuilder执行此操作:
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println(getReverse(s.nextInt()));
}
public static int getReverse(int a){
StringBuilder sb = new StringBuilder();
sb.append(String.valueOf(a));
sb = sb.reverse();
return Integer.parseInt(sb.reverse().toString());
}
}
显然,对于特定的情况(例如负数),这里存在一些差距,但我将留给您来解决。这应该是一个很好的起点。Java功能强大,但很复杂 这是它的单行线
public static void main(String[] args) {
int n = 123;
int reversed_n = (int) Integer.valueOf( // 5.) cast back to an int.
new StringBuilder(
Integer.toString(n) // 1.) Make the integer a String.
) // 2.) load that string to a String builder.
.reverse() // 3.) reverse the string.
.toString())); // 4.) go back to a string.
}
我尝试了一个相对完善的、外部库和无阵列的解决方案。希望有帮助
int reverseNumber(int inputNum)
{
String numString = Integer.toString(inputNum);
int numLength = numString.length();
int finalReversedNumber = 0;
/*
* Goes through the input number backwards, and adds the digit (with it's new value) to the final number.
* This is accomplished by multiplying the current number by 10 to the power of the index i, which corresponds
* to the current digit value of the number.
* i the index counter, goes from the length of the input number (not inclusive) to 0 (inclusive)
*/
for(int i=numLength-1; i>=0; i--)
{
int currentNum = Integer.parseInt(String.valueOf(numString.charAt(i)));
int valueInFinalNum = currentNum * (int)Math.pow(10, i); // i.e. a 5 in the thousands digit is actually 5000
finalReversedNumber += valueInFinalNum;
}
return finalReversedNumber;
}
有很多方法可以做到这一点。取决于是否要使用Java内置方法。以下是我的部分:
public class ReverseAListWithoutInbuiltMethods {
public static void main(String[] args) {
int[] arr={12, 4, 34, 7, 78,33, 20};
display(arr);
int[] rev=reverse(arr);
display(rev);
}
private static void display(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(" "+arr[i]);
}
System.out.println("");
}
private static int[] reverse(int[] arr){
List<Integer> list=new ArrayList<Integer>();
for(int a:arr){
list.add(a);
}
int[] rev=new int[arr.length];
int j=0;
ListIterator<Integer> listI=list.listIterator(arr.length);
while(listI.hasPrevious()){
rev[j]=listI.previous();
j++;
}
return rev;
}
}
public类ReverseAListWithoutInbuiltMethods{
公共静态void main(字符串[]args){
int[]arr={12,4,34,7,78,33,20};
显示(arr);
int[]版本=反向(arr);
显示器(rev);
}
专用静态无效显示(int[]arr){
对于(int i=0;i=0;i--){
charNew[j]=str[i];
j++;
}
返回新的;
}
private static char[] reverse2(StringBuffer sb) {
char[] str=sb.toString().toCharArray();
int n = str.length;
for (int i = 0; i < n / 2; i++) {
swap(str, i, n - 1 - i);
}
return str;
}
private static void swap(char[] charA, int a, int b) {
char temp = charA[a];
charA[a] = charA[b];
charA[b] = temp;
}
private static char[]reverse2(StringBuffer sb){
char[]str=sb.toString().toCharArray();
int n=str.length;
对于(int i=0;i
public static void reverse(int[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
int tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public class StackForStringReverse {
public static void main(String[] args) {
/*create a new string */
StringBuffer s= new StringBuffer("GeeksQuiz");
/*call reverse method*/
reverse(s);
/*print the reversed string*/
System.out.println("Reversed string is " + s);
}
private static void reverse(StringBuffer str) {
int n=str.length();
MyStack obj=new MyStack(n);
// Push all characters of string
// to stack
int i;
for (i = 0; i < n; i++)
obj.push(str.charAt(i));
// Pop all characters of string and put them back to str
for (i = 0; i < n; i++)
{
char ch = obj.pop();
str.setCharAt(i,ch);
}
}
}
class MyStack{
int size;
char[] a;
int top;
public MyStack(int n) {
top=-1;
size=n;
a=new char[size];//{'1','f','f','f'};
}
public boolean isEmpty() {
return top<0;
}
/**
* Insert data into Stack if it's not full*/
public void push(char c) throws IllegalArgumentException{
if(top>=size) {
throw new IllegalArgumentException("Stack overflow.");
}
a[++top]=c;
}
public char pop() {
if(top<0) {
throw new IllegalArgumentException("Stack underflow.");
}
char x=a[top--];
return x;
}
public void clear() {
while(top>0) {
top--;
size=top;
}
}
/**
* Display the data inserted */
public void display() {
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.println("");
}
}
公共类StackForStringReverse{
公共静态void main(字符串[]args){
/*创建一个新字符串*/
StringBuffers=新的StringBuffer(“Geeksquick”);
/*调用反向方法*/
反面;
/*打印反向字符串*/
System.out.println(“反向字符串为”+s);
}
专用静态无效反转(StringBuffer str){
int n=str.length();
MyStack obj=新MyStack(n);
//推送字符串的所有字符
//堆叠
int i;
对于(i=0;i return Top不发布图像。有一种方法可以添加代码。请使用实际代码编辑问题。如果将其视为字符串,则更容易。查看代码,您的返回类型似乎需要为[]likepublic int[]getreverse(int a)
。为什么将文本发布为图像不好:我能说什么呢?我不喜欢显式原语到对象强制转换。我喜欢显式。好吧,确实是这样。我现在知道你的意思了:)。谢谢大家的投入,这对纠正我的程序非常有帮助。我是这个论坛的新手,我能说的是,它真的很棒,很有帮助。嗨@PranavTyagi,如果一些答案解决了你的问题,不要忘记通过接受答案将你的问题标记为已解决