I'm studying Java. How do I store integers as arrays?

Asked 2 years ago, Updated 2 years ago, 48 views

You want to take an integer and sort it in ascending or descending order. If I get 152496, This integer is in array A[] A[0]=1 A[1]=5 A[2]=2 A[3]=4 A[4]=9 A[5]=6 Is there a way to save it like this?

Or is there another way to sort integers?

java sorting

2022-09-21 16:22

2 Answers

public class Example {
    public static void main(String[] args) {
        String input = "152496"; // Integer string entered
        int[] A = new int[input.length()]; // input Prepare array by string length.
        for(int i=0;i<input.length();i++) {
            When A[i] = input.charAt(i) - '0'; // i=0, '1' - '0' = 1.
        }
        //Output
        for(int i=0;i<A.length;i++) {
            System.out.println("A["+i+"]="+A[i]);
        }
    }
}    

If you want to do as you asked, you can do it as above.

When converting a char into a number, it can be obtained as the difference between Ascii, such as '1' and '0'.

To replace a numeric string consisting of more than one char (for example, "123") with a number: (Of course, even if it is a Korean character, it can be as follows.)

public class Example2 {
  public static void main(String[] args) {
    int value = Integer.parseInt("123");
    System.out.println("value="+value);
  }
}

.

One more thing, if the purpose is to decompose the input integer (int) into each digit... (without using a string)

public class Example3 {
  public static void main(String[] args) {
    int input =152496;
    // Calculating Digits
    int n,temp;
    n =0;
    temp = input;
    while(temp!=0) {
      n++;
      temp /= 10; // temp = temp / 10; 
    }
    // Create Array
    int[] A= new int[n];
    // Decompose each digit
    temp = input;
    for(int i=A.length-1;i>=0;i--) {
      A[i] = temp %10; // from the very end of the array, the lowest digit (one digit) of the number received is stored through the remaining operations.
      Temp /= 10; // divide by 10 so that the digit of 10 is 1.
    }
    //Output
    for(int i=0;i<A.length;i++) {
        System.out.println("A["+i+"]="+A[i]);
    }
  }
}


2022-09-21 16:22

public class Main {

     public static void main(String[] args){

         Scanner s = new Scanner(System.in);

         // variable declaration
         int num;
         int count = 0;
         int arrCount = 0;
         num=s.nextInt();


        // Find a multiple of 10 to divide the integer
         int i = 1;
         while(i<=num){
            i = i*10;
            count++;
         }
         i = i/10;


        // Declare the array and divide it by the above i and put it in the array
         int[] arr = new int[count];
         while(num>0){
         arr[arrCount] = num/i;
         num = num%i;
         i = i/10;
         arrCount++;
         }
        //Output
        for(int j = 0 ; j<arr.length;j++){
             System.out.println(arr[j]);
    }

}


2022-09-21 16:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.