Jump to content

Swapping Values in an array - Java

CalebTheEternal

I am in an AP Computer Science class at school and we are learning Java and I'm not sure how to do this.

I need to "Swap the values at positions i and j in the array"

 

That is all of the info I have. 

 

My teacher is a Math teacher and can't explain this to save his life. 

 

Thanks for the help.

CPU: Ryzen 5 5600 Motherboard: MSI B550 Tomahawk RAM: 32Gb DDR4  GPU(s): MSI 6800-XT Case: NZXT H440 Storage: 4x 250gb SSD + 2TB HDD PSU: Corsair RM850x with CableMod Displays: 1 x Asus ROG Swift And 3 x 24" 1080p Cooling: H100i Keyboard: Corsair K70 RGB Mouse: Corsair M65 RGB Sound: AKG 553 Operating System: Windows 10

 

Current PC: 

http://i.imgur.com/ubYSO3f.jpg          http://i.imgur.com/xhpDcqd.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

Do you know how to get a value from an array?

Do you know how to modify a value in an array?

 

If so you're most of the way there.

Not at all. Care to explain how? 

CPU: Ryzen 5 5600 Motherboard: MSI B550 Tomahawk RAM: 32Gb DDR4  GPU(s): MSI 6800-XT Case: NZXT H440 Storage: 4x 250gb SSD + 2TB HDD PSU: Corsair RM850x with CableMod Displays: 1 x Asus ROG Swift And 3 x 24" 1080p Cooling: H100i Keyboard: Corsair K70 RGB Mouse: Corsair M65 RGB Sound: AKG 553 Operating System: Windows 10

 

Current PC: 

http://i.imgur.com/ubYSO3f.jpg          http://i.imgur.com/xhpDcqd.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

Not at all. Care to explain how? 

 

 

x is your array

to get the value at i

x

 

to modify a value

 

x = 2

 

There, you're most of the way there.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Let's first look in how to swap two values :

int a = <value>;int b = <value>;int temp = a;a = b;b = temp;

Then you just do that with the elements at the given positions.

 

void swap(int *v, int i , int j){    int temp = v[i];    v[i] = v[j];    v[j] = temp;}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

An array is like a list of values. So, let's say you have a String array called ar, with two values, "hello" and "there", like so:

String[] ar = {"hello", "there"};

In order to swap "hello" (in position 0) and "there" (in position 1), you need to obtain the value from the first position and assign it to the second position, and take the value from the second position and assign it to the first position. In this case that would mean taking "hello" and using it to overwrite "there", and then taking "there" and using it to overwrite "hello".  Like this:

String[] ar = {"hello", "there"};ar[1] = ar[0];ar[0] = ar[1];

But there's a problem. if you overwrite one of the values, how are you going to use the overwritten value which no longer exists in the array? Try to find the solution to that yourself. 

 

Edit: I sympathize with your situation. When I took AP Computer Science, the actual teacher left the school two weeks before school began to be a principal at another school, so we had a year-long substitute who was a biology major and knew little about coding. Our school ended up paying for an online class, with online materials and an online teacher. 80% of the students failed anyway. 

Edited by Raymondbl
Link to comment
Share on other sites

Link to post
Share on other sites

In this method we are basically swapping two positions in the array.

public static void swap(String[] arr, int x, int y){		//We have to store value in the index of x because we are about to over right thatString temp = arr[x];//We then set what is in the array at position x to be what is in the array at position yarr[x] = arr[y];//Finally we then have to set what is in position y to be temp.arr[y] = temp;}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×