简体   繁体   中英

How do I make a for loop that would go through a prior method?

My current code is in two methods. I am trying to get the first position to switch with the second. For example, some on input 6 & 7. I want that to become 7 & 6, but in my case they are predefined, and are more than two single digits.

My first method:

static void nameSwitch (int[] array, int pos1, int pos2 ) {

    int temp = array[pos1];       /*As you all may know, this is the switching of 
    array[pos1] = array[pos2];    position 1 and 2.*/
    array[pos2] = temp;

}

My Second Method:

static void lastToFirstName() {
    int [] values = {1,2,3,4,5,6,7,8,9,10};
    for(//this is where I am stuck at.)


    System.out.println(Arrays.toString[values]);
 }

I want to output to be [21436587109], and the issue with me is getting to implement the method in the other method and getting the loop correct.

So it appears to me that you are switching every other term in values .

Original input:

int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

This can be accomplished in a for loop structured similar to this which 'jumps' to every other term and performs the switching operation accordingly:

for (int i = 0; i < values.length; i += 2) {
    nameSwitch(values, i, i + 1);
}

Final output:

values = {2, 1, 4, 3, 6, 5, 8, 7, 10, 9};

Edit: Adding compare functionality to the nameSwitch() method

You could edit nameSwitch() to take into consideration which number is greater; if pos1 is greater than pos2 , the switch would be conducted. Otherwise, pos1 is less than pos2 and the method would be immediately returned.

static void nameSwitch(int[] array, int pos1, int pos2) {

    if (pos1 < pos2) {
        return;
    }

    int temp = array[pos1];
    array[pos1] = array[pos2];
    array[pos2] = temp;

}

If I understand your question this code will work

static void nameSwitch (int[] array, int pos1, int pos2 ) {
    int temp = array[pos1];       /*As you all may know, this is the switching of position 1 and 2.*/
    array[pos1] = array[pos2];    
    array[pos2] = temp;

}
public static void main(String[] args) {
    int [] values = {1,2,3,4,5,6,7,8,9,10};
    int i;
    for(i = 0; i < values.length; i+=2) {
        nameSwitch(values, i, i+1);
    }
    System.out.println(Arrays.toString(values));
 }

Try this,

 for(int i=0;i+1<values.length;i=i+2)
 {
    nameSwitch(values, i, i+1);
  }

Yo need to increment i by 2 in each iteration so the next pair of numbers can be swapped with each other. i+1<values.length will ensure that if you have only one element remained for last iteration then it is kept as it is.

public class Switchnumbers {
public static void main(String[] args) {
    lastToFirstName();
}

static void lastToFirstName() {
    int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    for (int i = 0; i < values.length; i = i + 2) {
        int tmp = values[i];
        values[i] = values[i + 1];
        values[i + 1] = tmp;
    }
    for (int i = 0; i < values.length; i++) {
        System.out.println(values[i]);
    }

}

}

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