last day (14 days later) » 

1:09 AM
0
A: Writing into an array

Tahar BakirIn this for loop you create a new ArrayList for each iteration. List<Integer> yearsList = null; for (int i = 0; i < rainfallYears.length; i++) { yearsList = new ArrayList<Integer>(); yearsList.add(new RainfallYear(i, null).getYear()); } int[] yearsArray = new int[yearsList.size()]; for ...

 
Hey, thanks for the answer. Are you suggesting I should get rid of the ArrayList completely?
 
well it seems overkill since you're only taking the values beetween 0 and rainfallYears.length so if those are the values you want to put in the array then yes.
 
But in the third line of your code you are referring to yearsList.get(i).
 
yes, my bad, wrong copy paste. corrected it
 
Sorry for asking so much, but shouldn't there be somewhere a new RainfallYear within the code, since we are making a new object and using the method getYear on it?
 
1:09 AM
like I said in the answer in the new RainfallYear(i,null) you are putting i, as the year then you get the year wich will give you back i. so that RainfallYear seems useless there
yo
you there ?
 
I understood what you are saying, but shouldn't somewhere in this method there be a case where I'm using the getYear() method? We have created an array, but instead of having "i " inside I need RainfallYear.getYear()
Or something along those lines right? Thank you for your time, I'm just really stuck with this for a few days now.
 
let take it step by step
what you want is put the years in the int array
 
yes
 
ox
but the first loop you do nem RainfallYear(i,null)
i going frow 0 to rainfallYears.length
lets say rainfallYears.length is 100
you're creating 100 RainfallYear objects
with the years 0,1.2.... 100
so the first time it will be RainfallYear(0,null)
and if you do new RainfallYear(0,null).getYear()
that will give you 0
since your code is
public RainfallYear(int year, double[] rainfallMonths) {
super();
this.year = year;
this.rainfallMonths = rainfallMonths;
}
 
yes, I understand that.
 
1:17 AM
ok then why don,t you just skip the object creation
since i has already the value you want
 
Because my years start from 1914 till 2008
 
and that wont change ?
always 1914-2008
?
 
yr
yes
in theory, I can create 2, 5, 10 RainfallYear objects, e.g.
1922,107.2,95,69.6,77.4,33.2,39.6,119,96.6,80.6,33.1,47.1,118.7
1923,62.4,153.5,48.3,67.9,67.3,20.5,76.7,95,82.9,137.2,92.5,96.8
1924,91.3,24.3,36.2,73,122,63.1,110.4,96.3,111.9,126.9,73,123.3
1925,79.2,124,31.2,71,100.5,4,69.3,86.7,95.1,106,70.3,94.8
1926,110.6,78.3,24.5,65.4,67.1,73,73.6,71.5,53.9,86.6,162.1,23.3
1927,90.8,76.8,86.8,56.8,35.8,98.7,96.2,135,142.3,76,100.5,77.2
1928,160.7,81.3,76.7,39,37.1,98.4,56.9,93.1,32.7,142.2,121.4,84.9
And what I should get from the int[] method in the end is [1922, 1923,...1928]
 
ok so you don't create the objects with the for loop
where do you get those values ?
 
main class
 
1:21 AM
ok so
something that looks like
 
RainfallYear [] temp = new RainfallYear[10];
temp[0] = new RainfallYear(1914, new double []{ 50.9,87,115.8,32.3,47.1,56.6,97.1,63.9,48.1,62.4,110.3,190.8});
temp[1]= new RainfallYear(1915, new double []{105.3,129,36.2,37.3,64.3,33.3,123,76.2,39.4,71.4,73.5,182.7});
temp[2] = new RainfallYear(1916, new double []{58.6,129.4,97.9,47.9,70.7,63.7,51.3,89.2,54.8,142.1,110.1,90.4});
temp[3]= new RainfallYear(1917, new double []{58.2,30.9,67,55,61.7,65.7,64.3,172.8,54.8,130.7,61.7,43.1
});
temp[4] = new RainfallYear(1918, new double []{ 90.1,67.8,39.2,56,57.7,31.5,110.1,60.7,186.2,72.1,67.3,123.2
this is from a test file.
 
ok i see
so you read the file
and put the values in the temp array
and you want to got the years for that array and put them in a separate array
is that it?
 
yes
just have an array with years, extracted from the temp array objects.
 
ok so the reading and initializing of the temp array
that's already done
right?
?
 
yes
 
1:26 AM
then you anly need this
int[] yearsArray = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
yearsArray[i] = temp[i].getYear();
}
only*
since temp is an array of RainfallYear objects
 
One second, can I quickly check whether this solution works.
 
temp[i] will give you a RainfallYear object
on wich you can call the get
yeah of course
i'll wait
 
Thank you. So much!!!
 
works ?
 
Oh my god this looks so simple, how could I miss it...
Yes, works perfectly, thank you so much!
 
1:29 AM
happens to the best :)
 
I will upvote your comment and give you the right answer. Thank you once again.
 
no problems that what SO is for
 
:) have a nice evening
 
you too
 

last day (14 days later) »