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

Trying to refer to a final ImageView does not work
Connect to tor with java - TorLib
Why won't this sorting work
First random number after setSeed in Java always similar
When override a method, say(), and then run the program I do not get what I anticipated to be printed
doClick(), and Simon: All buttons will unpress at the same time instead of individually
Java Algorithms: comparing each *thing* to every other [closed]
Whats causing this NullPointerException in AWT-EventQueue-0?
Gradle + Dropwizard + Shadow -> Could not find or load main class
Android - Try to send fake sms to myself without mobile network usage
Selenium, open new window
Using Android TCP Client app to interface with sensor
Normal distribution function:determine probability of a given point in Java
Set object reference to null or call the finalize() method?
Are there alternatives to String.format that use cached formatting? [closed]
Can't register portlets on Liferay

Categories

HOME
google-chrome-extension
variables
mfc
homebrew
mean-stack
rsyslog
framework7
sqlite-net-extensions
cmd
graphql
jpeg
ip
podio
multiple-records
slick-slider
wheelnav.js
designer
sms-gateway
code-review
autocad-plugin
java-3d
functional-testing
web-sql
google-search-api
shopware
javacv
tooltipster
fifo
fluentvalidation
neo4j-spatial
opentype
windows-server-2000
newline
exuberant-ctags
google-qpx-express-api
subset-sum
android-kernel
atl
nssegmentedcontrol
android-ble
revolution-slider
plsql-psp
vxworks
jquery-bootgrid
libraries
code-search-engine
s
skeleton-css-boilerplate
google-datalayer
celery-task
media-player
r-forge
capacity
spring-mongodb
bind9
wdf
sts-springsourcetoolsuite
elements
trash
thrust
django-debug-toolbar
qpid
php-internals
tform
natvis
t4mvc
thredds
riak-cs
method-parameters
apache-commons-fileupload
composite
e10s
rake-task
tween
dd
codeigniter-url
socketexception
castle
starcluster
ember-charts
quantlib-swig
ruby-datamapper
dbconnection
cos
padarn
plasma
spring-validator
eventlistener
enterprisedb
jelly
distutils
pvrtc
word-processor
dbproviderfactories
online-compilation
postgresql-performance
bigcouch
subscript
heartbeat
coredump
ecl
cxxtest
asp.net-mvc-areas
substrings
brewmp
microsoft-virtualization
boost-smart-ptr
text-coloring
ubuntu-9.04
lzh
ajax-forms

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html