Hello I am in my second year as a college student taking my 2ndclass as a Comput
ID: 3618753 • Letter: H
Question
Hello I am in my second year as a college student taking my 2ndclass as a Computer Science major. In my class we are currentlylearning different ways to use arrays. Below is a series of codesof how to use arrays in different ways. My problem is figuring outthe part of the code where I guess the array is output in reverse(if you understand what I mean its the "public static int[]reverseA(int[] a)" part ). If it helps understanding my code bettermy class writes it from NetBeans IDE 6.7 and we use Java.please and thank you for your help in advanced
package morearrays;
public class Main {
public static void printArray(int[] a){
for (int i = 0; i <a.length; i++ ){
System.out.println(a[i]);
}
}
public static void copyArray(int[] a){
int[] a2 = newint[a.length];
for (int i = 0; i <a2.length; i++) {
a2[i] = a[i];
}
printArray(a2);
}
public static void compareArrays(int[] a, int[]b){
boolean equal =false;
if(a.length == b.length){ equal = true; }
int i=0;
while((i < a.length)&& equal) {
if(a[i] != b[i]) { equal = false; }
i++;
}
if (equal){System.out.println("they are EQUAL");
}else {
System.out.println("they are NOT EQUAL");
}
}
public static void findHL(int[] a){
int lowest = a[0],highest = a[0], temp =0;
for (int i = 0; i <a.length; i++) {
temp = a[i];
if (temp > highest) { highest = temp; }
if (temp < lowest) { lowest = temp;}
}
System.out.println("high= "+highest+" lowest ="+lowest);
}
public static int[] reverseA(int[] a){
int[] b = newint[a.length];
int dec =a.length-1;
for (int i = 0; i <b.length; i++, dec--){
b[dec] = a[i];
}
return b;
}
public static void main(String[] args) {
int[] a1 = {1, 2, 3, 4,5,};
int[] a2 = newint[a1.length];
for (int i = 0; i <a2.length; i++){
a2[i] = a1[i];
}
printArray(reverseA(a1));
}
}
Explanation / Answer
please rate - thanks hope this helps public static int[] reverseA(int[] a){ //allocate a new array to get the reverseddata int[] b = newint[a.length]; //dec has 1 less then the number of elements in ain it, since indices are 0 to 1 less then the number ofelements int dec =a.length-1; //starting at the 1st element of a and the lastelement of b (I think this is where yourproblem must be) //each iteration i is incremented, so you are going through a0,1,2,3..., at the same time dec is decremented, so you //are going through b backward, dec,dec-1,dec-2... thusputting a into b backward (reversing thearray) for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.