简体   繁体   中英

I'm just having hard time understanding the class material from my java class

Can someone explain this code line by line. I just started to learn how to code and I'm learning java right now,but I'm having hard time how this function executes. What does new int[10] ; really do? and I don't really get the difference or how repeatedDigits[index] part is used differently from the repeatedDigits[remainder] part.

Write a function that counts the repeated digits in the integer x.

For * example : if x is 999, then your function must print back that 9 occurs 3 * times . Another example, if the integer x is 992121, then your function * must print back that 9 occurs 2 times, 2 occurs 2 times and 1 occurs 2 * times . This was a guideline for the function.

public static void countRepeatedDigitsInANumber(int x) {

int[] repeatedDigits = new int[10];
while (x > 0) {
int remainder = x % 10;
x = x / 10;
repeatedDigits[remainder] = repeatedDigits[remainder] + 1;

}

int index = 0;
while (index < 10) {

if (repeatedDigits[index] != 0)
    System.out.println("The digit " + index + " occurs " + 
    repeatedDigits[index]);
index++;

    }
}

This is a code that compiles. I'm just posting this because I'm having hard time to understand. I would realy appreciate if someone could give any suggestion to learn java quickly as well!

What does new int[10];

It creates a new array of size 10 that can hold int values. All of the values in the array will be initialized to 0 (this is just how Java handles int primitives).

In this case, it looks like this array is being used to store the number of times that the digit corresponding to its index has been seen.

Hmm, that was confusing to type, and probably even more confusing to read, so let me use an example. For the input 992121 , you'd expect the repeatedDigits array to look like this when the program is done:

[0, 2, 1, 0, 0, 0, 0, 0, 0, 2]

ie Two 1s, one 2, and two 9s


while (x > 0) {

This starts a loop that will keep going until x is equal to (or smaller than, but that shouldn't happen) zero.


int remainder = x % 10;

This essentially "captures" the right-most digit of the number using modular arithmetic. It divides the number by 10, and any remainder is going to be what's in the "ones place" (ie the right-most digit).


x = x / 10;

This shifts digits to the right by one, and throws away the right-most digit (since you've already captured it).


repeatedDigits[remainder] = repeatedDigits[remainder] + 1

This just increments the value in the array at the appropriate location. For example, if you just pushed a 9 off the right end of the number, this will increment the value in repeatedDigits[9] so you know that you just saw an additional 9.


int index = 0;
while (index < 10) {

We're about to do some looping! There are (in my opinion) better ways to do this, but this works just as well. Basically, you want index to start at 0, and loop all the way up to 9, but stop when you get to 10.


if (repeatedDigits[index] != 0)

If the value at a given location in the repeatedDigits array is not zero, then...


System.out.println("The digit " + index + " occurs " + repeatedDigits[index]);

... print it out!


index++;

And finally, increment index so we can check the next number. This is exactly the same as index = index + 1 .

Welcome to SO and the beautiful world of Java! I've attached hyperlinks all throughout my answer that could possibly help you out.

int[] repeatedDigits = new int[10];

The above line of code is creating an array with 10 elements, from index: 0 to index: 9.

int remainder = x % 10;

The above line of code is calculating the remainder once 'x' has been divided by 10. For example, if 'x = 9192' then it would be dividing '9192 / 10 = 919.2' And taking the remainder. Which in this case is the '2'. Notice how it's actually looking at the number in reverse? So a number like '9192' is actually being processed as '2919'.

x = x / 10;

The above line of code is dividing the number down by 10-since it's integer division it drops all decimal values. Since we have already calculated the last number. So our '9192' is now just '919'.

repeatedDigits[remainder] = repeatedDigits[remainder] + 1;

The above line of code is going to the index of the array of the remainder value that we stored earlier. Remember how our remainder was '2'? It's not going to index: 2 in our array and adding 1 to it's value- which was originally 0. So now our array at index: 2 has the value: 1. Meaning the number 2 was repeated 1 time.

What I've laid out above is just 1 iteration of the while loop that the top half of your code is in. So get a piece of paper and redo everything I did above with the remaining number '919'.

Once you're finished you should notice that your array looks something like this:

index 0: 0
index 1: 1
index 2: 1
index 3: 0
index 4 to 8: 0
index 9: 2

This means that '1' came once. '2' came once. and '9' came twice.

As for advice on learning Java? Read a lot. Google every little question you have, and if you don't understand how something is working get a piece of paper and write it out.

int[] repeatedDigits = new int[10];

//create an array of integers of size 10

while (x > 0) { 

//while the integer provided is greater than 0

int remainder = x % 10;

//gets the smallest value digit in a base ten system. 101%10 =1

x = x / 10; 

//in base ten this reduces the size of the number by a tens place. 101/10 =10 as 10 can be fully divided into 101 10 times. You've dropped the last digit of the number in this way. This is what will allow you to exit the while loop, as eventually x will be equal to 0.

repeatedDigits[remainder] = repeatedDigits[remainder] + 1;

remainder here can be between 0-9. This is because remainder is the remainder of dividing the provided integer by 10. You can't divide a number greater than 0 by ten and have a value less than zero or greater than 9.

The array you created has indexes 0-9, as it is ten spaces long (0,1,2,3,4,5,6,7,8,9). You are assigning the value of the index of repeated Digits that corresponds to your remainder to whatever it was previously plus 1.

What this means is you have found that the last digit in the number is equal to some value X. X must be between 0-9. As you have an array with indexes between 0-9 you get the value associated with repeatedDigits[X], add one to it as you have found another value of X, and set repeatedDigits[X]'s value equal to what is was before you found this new X plus one.

}

int index = 0;

You start at the first value of an array (0)

while (index < 10) {

Go through the values of the array from 0 to 9

if (repeatedDigits[index] != 0)

if the value of the array repeatedDigits at index is not 0. This is the same as saying if there was a value of index found in the input number.

    System.out.println("The digit " + index + " occurs " + 
    repeatedDigits[index]);

index is the number we looked at. The count of how many times it occurred is stored at repeatedDigits[index]. We are printing the value and how many times it occurred.

index++;

Increase the index, examining the next index value.

    }
}

Let me see if I can clear a few things up for you. My first suggestion is to format your code so that the lines within loops are indented:

public static void countRepeatedDigitsInANumber(int x) {

  int[] repeatedDigits = new int[10];
  while (x > 0) {
    int remainder = x % 10;
    x = x / 10;
    repeatedDigits[remainder] = repeatedDigits[remainder] + 1;
  }

  int index = 0;
  while (index < 10) {
    if (repeatedDigits[index] != 0)
      System.out.println("The digit " + index + " occurs " + repeatedDigits[index]);
      index++;
    }
  }
}

This helps us to see the structure of the program. We can now see that the program runs as follows:

  1. Define a new int array: int[] repeatedDigits = new int[10];
  2. Run a while loop: while (x > 0) { ... }
  3. Define an int variable: int index = 0;
  4. Run a second while containing an if statement: while (index < 10) { if ( ... ) { ... } }

I'll try to explain each of the four pieces one at a time.

  1. First we define our int array. new int[10] defines an array with ten spots in it, each of which can hold an integer. You can think of this like ten boxes next to each other, each labeled with an index. This array will be used to keep track of how many times we've seen each integer. repeatedDigits[0] will hold the number of times we've seen a 0 digit in our number; repeatedDigits[6] will hold the number of times we've seen a 6 digit, and so on.

  2. Here's the heart of the program. We're going to break down the number, one digit at a time, and for each digit, we add one to the spot in the array used to keep track of how many times we've seen that digit. This line gets us the rightmost digit in our number: int remainder = x % 10; . % is called the modulo operator, and you can search for that to learn more about it. With the input 992121 , this will assign remainder to 1 .

The next line uses integer division to remove that last digit from the number, so that the next time through the loop we get the next digit. if x = 992121 then after x = x / 10 , x will equal 99212 .

Finally, we add one to the spot in the repeatedDigits array corresponding to the digit we saw. So in our example, remainder = 1 , so we add 1 to the repeatedDigits[1] spot. This line takes advantage of the fact that an uninitialized int defaults to 0 .

We've reached the end of the while loop, so we check the condition, and find that x > 0 , so we run the loop again. We repeat this for every digit in the input number`.

The hard work is now done; the rest of the program is there to print out the results.

  1. We assign index to 0. The next while loop will increment this variable from 0 to 9 , printing the value of the repeatedDigits array for each digit.

  2. We run the loop from index = 0 to index = 9 . For each value, we check to see if we saw that digit in our input number (that's the if statement). We check the value in repeatedDigits[index] to see if it doesn't equal 0 . If so, then we must have incremented it in the previous loop, so we know that digit appeared at least once in the original input. In that case, we print a line to the console using System.out.println . Then we increment index and start the loop again.

Please let me know if you have any questions, and good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM