“membalikkan array” Kode Jawaban

bagaimana membalikkan array

        int[] xr = {1, 2, 3, 4, 5};

        System.out.print("[");
        for (int i = xr.length - 1; i >= 0; i--) {
            System.out.print(xr[i] + ", ");
        }
        System.out.println("\b\b]");
    }
Chathumal Sangeeth

cara membalikkan array di javascript

// reversing an array in javascript is kinda hard. you can't index -1.
// but i can show how you can do it in 4 lines.

var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (var i = myArray.length - 1; i > 0; i -= 1) {
	myArray.shift();
	myArray.push(i);
}

console.log(myArray); // output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
tomiha

Array terbalik dalam array baru

#include <iostream>
#include <iomanip>

using namespace std;

void reverseArray(int array[],int newArray[],int size);
void printArray (int array[],int size);

int main()
{
    int list [6],newList[6];
    for(int i=0;i<6;i++){
        cout<<"Element "<<i<<": ";
        cin>>list[i];
    }
    
    reverseArray(list,newList,6);
    cout<<"Original array: \n";
    printArray (list,6);
    cout<<endl<<"Reversed array: \n";
    printArray (newList,6);
    
}

void reverseArray(int array[],int newArray[],int size){
    for(int i=0,j=size-1;i<size;i++,j--)      
        newArray[j]=array[i];
}

void printArray (int array[],int size){
    for(int i=0;i<size;i++)
        cout<<setw(5)<<array[i];
}
S.Y

membalikkan array

const list = [1,2,3,4,5];
list.reverse(); // It changes the original array

console.log('reversed:', list);
// expected output: "reversed:" Array [5, 4, 3, 2, 1]
Scriper

membalikkan array

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
naly moslih

cara membalikkan array di javascript

numArr.reverse();
strArr.reverse();

console.log(numArr);
console.log(strArr);
Shubham Rajput

Jawaban yang mirip dengan “membalikkan array”

Pertanyaan yang mirip dengan “membalikkan array”

Lebih banyak jawaban terkait untuk “membalikkan array” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya