004-004-008

Array Assignment: Swapping Elements

Medium

Problem Description

Array Assignment: Swapping Elements

Learning Objective: Understand array element swapping and master safe swapping using temporary variables

Overview

When swapping two elements in an array, a temporary variable (temp) is used. Direct assignment would cause the original value to be lost.

Specifications

Implement the following:

  1. Declare an integer array {10, 20, 30, 40, 50}
  2. Swap the first element (index 0) with the last element (index 4)
  3. Use a temporary variable temp for the swap
  4. Output the swapped array in "10 20 30 40 50" format (space-separated)

Output Format

50 20 30 40 10

Hint

  • Three assignment statements are needed for swapping
  • Use the pattern: temp = a; a = b; b = temp;

Test Cases

※ Output examples follow programming industry standards

Input:
Expected Output:
50 20 30 40 10
Input:
Expected Output:
50 20 30 40 10
Input:
Expected Output:
50 20 30 40 10
Input:
Expected Output:
50 20 30 40 10
❌ Some tests failed
❌ エラー発生

Your Solution

Current Mode: My Code
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here

sc.close();
}
}
0 B / 5 MB

You have 6 free executions remaining