Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. write a method to check to see if the elements in an int array are always increasing.
- public static boolean arrayAcsending(int[] nums){
- boolean isAscending=true;
- in count=1;
- while(isAscending&&count<nums.length){
- if(nums[count]<nums[count-1]){
- isAscending=false;
- }
- }
- return isAscending;
- }
- 2. reverse an array
- public static void reverseArray(int [] nums){
- /*SWAP!
- step 1: create a temp variable and store element at current index
- step 2: assign value from index length-i-1 into current index
- step 3: assign value from the temp variable into index length-i-1
- */
- for(int i=0; i<nums.length/2; i++){
- int temp=nums[i];
- nums[i]=nums[nums.length-i-1];
- num[nums.length-i-1]=temp;
- }
- }
- 3. shuffle an array
- public static void shuffleNums(int[] nums){
- for(int i=0; i<nums.length-1; i++){
- //get min and max for generating rand range
- int max=nums.length-1;
- int min=i;
- //generate rand
- int rand=(int)(Math.random()*(max-min+1))+min;
- //swap nums[i] with nums[rand]
- int temp=nums[rand];
- nums[rand]=nums[i];
- nums[i]=temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement