0% found this document useful (0 votes)
5 views1 page

Exo 6 Tableau

Uploaded by

hcopinion963
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Exo 6 Tableau

Uploaded by

hcopinion963
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Arrays;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {


int[] sortedArray = {2, 14, 16, 19, 23, 28, 34, 46, 0, 0, 0};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

int i = 0;
while (sortedArray[i] < num && i < sortedArray.length - 1) {
i++;
}

for (int j = sortedArray.length - 1; j > i; j--) {


sortedArray[j] = sortedArray[j - 1];
}

sortedArray[i] = num;

int indexOfZero = 0;
while (sortedArray[indexOfZero] != 0) {
indexOfZero++;
}

int[] newSortedArray = new int[indexOfZero];


System.arraycopy(sortedArray, 0, newSortedArray, 0, indexOfZero);

System.out.println("New sorted array: ");


System.out.println(Arrays.toString(newSortedArray));
}
}

You might also like