java


Could anyone explain how its work? [duplicate]


I'm trying to understand the following Java exercise. Even running the debugger I don't understand the details of the second and third printout:
1, 2, 3, 4
1, 2, 4, 4
1, 2, 4, 8
I understand that the first print is the array as it is, second line prints [2] element of the array and third line [3] element. Here is the code:
public class TR1
{
public static void main(String[] args)
{
int[] v = {1, 2, 3, 4 };
print(v);
x(v, v[2] - 1);
print(v);
x(v, v[3] - 1);
print(v);
}
public static void x(int array[], int y)
{
array[y] = array[y - 1] * 2;
}
public static void print(int array[])
{
System.out.print(array[0]);
for (int i = 1; i < array.length; i++)
System.out.print(", " + array[i]);
System.out.println();
}
}
Let's see what this method does :
public static void x(int array[], int y)
{
array[y] = array[y - 1] * 2;
}
It takes the value at index y-1, multiplies it by 2, then assigns this result to the index y .
Starting array : {1,2,3,4}
The call with v[2] - 1 takes the value at index 2 (which is 3), and substracts 1, so we have y = 2.
From what we said before, the method takes the value at index 1 (y-1) which is 2, multiplies it by 2 so we get 4, and assigns that to the index 2 (y) .
Current array : {1,2,4,4}
The call with v[3] - 1 takes the value at index 3 (which is 4), and substracts 1, so we have y = 3.
From what we said before, the method takes the value at index 2 (y-1) which is 4, multiplies it by 2 so we get 8, and assigns that to the index 3 (y) .
Current array : {1,2,4,8}
Lets briefly walk you through the first things that happen. You start with this array:
1 2 3 4
No surprise when printing that.
Then you run:
x(v, v[2] -1 ) ... evaluates to
x(v, 3 - 1) ... evaluates to
x(v, 2)
Which changes the array based on:
array[y] = array[y - 1] * 2;
Lets insert y as 2 (see above):
array[2] = array[1] * 2;
array[2] = 2 * 2;
leading to:
1, 2, 4, 4
So, the real answer is: you don't even need a debugger. A piece of paper, a pen and a bit of thinking is even more efficient to uncover the "secrets" here.
print always prints the entire array. x and y are making changes to the array in between.
Keep in mind that v[x] is just an integer. For example, v[2] is initialy just 3, so v[2] - 1 = 2. Hence it modifies v[2] between the first and second call to print.

Related Links

How to add and overlap two images with X,Y cordinates
Javax.mail.AuthenticationFailedException
Which emoji can be used as variables in Java, visible in Eclipse?
i am a beginner in java !! my doublt is regarding = and new operator in case of objects
Java “hello world” over the internet (RMI, RMI-IIOP, EJB) [duplicate]
Volatile array of arrays
Why isn't the frame centred in this GUI program when it is run?
how to read an image from a url and convert it from CMYK to RGB in java?
init parameters in web.xml not accessed in jsp
GridBagLayout and BorderLayout on JPanel Aligning
dyld: Symbol not found: _iconv when using javac to compile on MacOS
soap messages are not visible in tcp/ip monitor
list available java packages and classes within clojure
Libgdx - Loading fonts slows down game
How to parse Google Protocol Buffers that in the direct memory without allocating heap byte array in Java?
Android Realm: compactRealmFile NullPointerException

Categories

HOME
java
variables
date
debugging
hp-exstream
react-router
youtube-dl
v8
flyway4
azure-media-services
django-imagekit
pheatmap
webrequest
iggrid
gnupg
ssl-client-authentication
quicklook
php-7.1
zebra-printers
netflix
systemc
libtiff
code-review
clickonce
mmap
firebase-crash-reporting
xpages-ssjs
wpfdatagrid
nhapi
opentype
saas
galsim
twilio-api
pdb
siesta-swift
calibre
windows-dev-center
wallpaper
hexo
fusionpbx
dosbox
swisscomdev
lto
withings
starteam
bootstrapper
awt
preconditions
powershell-dsc
glew
http-redirect
jquery-nestable
statsd
mplayer
setter
libusb-win32
font-size
sybase-asa
namecoin
spring-mongodb
pycaffe
watchconnectivity
xcb
setuptools
fody
blackberry-10
superstack
rhino-servicebus
associative-array
r-tree
retina
browser-bugs
drawbitmap
responsive-images
graph-drawing
generic-programming
nstableviewcell
tt-news
gui-test-framework
codeigniter-routing
asp.net-web-api-odata
websocket4net
mesa
uitouch
reactfx
didselectrowatindexpath
imdbpy
quantlib-swig
web2py-modules
opcache
jplaton
android-screen-support
anonymous-methods
automount
errai
asyncfileupload
coercion
file-comparison
celltable
floating
visitor-statistic
sproutcore-2
icanhaz.js
modelstate
nsviewanimation
pascal-fc

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile