+ 1

Assign returned 1d array to a 2d array in java

So my question is something like this...I made a function which takes an integer value and creates an array of group of nos(function not important) and the function returns the integer array...I have to repeat this four times and put it in a 2d jagged array.. The function name is tutors() I declared 2d array as int ta[][]= new int[4][]; What I used was int ta[0]=tutors(22); But it gave me an illegal start of expression error..pls help me regarding this. EDIT:here is my existing code and the question.. https://code.sololearn.com/cMAZxCeXJX32

12th Jan 2019, 1:59 PM
Mystic77
Mystic77 - avatar
10 Réponses
+ 1
Yes, you've learned about arrays probably hence this assignment? if you want to return an array you have to do it as type[] function. This wasn't the main reason for rewriting your program as there were a lot more errors; I'll try to go over some of them in your code: line 54~57; assigning a jagged array is done like this ex: array[0] = new int[2] or parse it an array for that matter; NOT like this array[0][]; you can give the array a value like that though ex: array[0] = new int[1]; array[0][0] = 10; line 58~61 (this is where you're parsing them) the Display function, you're trying to parse a single array to it but you have a jagged array, this is wrong; you can just parse it the jagged array like this: Display(array); no need to specify an index and in your methods or functions, you should do it like this: int[][] varname your methods / functions, they need to be static, put static in front of them if you prefer your code that I fixed over the one I wrote, while writing this I quickly went over it to explain you what was wrong, you can find it here: https://onlinegdb.com/By5ijswz4 If you got any further questions, feel free to ask! Hope this helps.
12th Jan 2019, 6:02 PM
Joery De Loose
Joery De Loose - avatar
+ 2
Would you mind sharing your existent code as this would probably help to assist you further anyway, a problem with jagged arrays? Here's a way to declare them; int[][] JaggedArray = new int[3][]; //store them inside the top array JaggedArray[0] = new int[2]; JaggedArray[1] = new int[2]; JaggedArray[2] = new int[2]; OR JaggedArray[0] = new int[] {1, 2, 3}; JaggedArray[1] = new int[] {1, 2, 3}; JaggedArray[2] = new int[] {1, 2, 3}; Here's a small example in java how it works: https://onlinegdb.com/H1S3c_PMV an example that uses a function to assign it: https://onlinegdb.com/Syc4AODzV Hope this helps;
12th Jan 2019, 2:25 PM
Joery De Loose
Joery De Loose - avatar
+ 1
Full code please.
12th Jan 2019, 2:21 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Still not a hundred percent sure what the code is supposed to do, I rewrote the majority of it besides the tutors function; Can you check if it's the expected output? (ignore line 35 and 36, debugging purposes) https://onlinegdb.com/SJ__dcDMN
12th Jan 2019, 4:41 PM
Joery De Loose
Joery De Loose - avatar
+ 1
static void function(int[][] arr) just parse the function the whole Jagged array, after that you can still only manipulate a single row
13th Jan 2019, 9:44 AM
Joery De Loose
Joery De Loose - avatar
+ 1
you’re welcome, feel free to ask anything else, I’ll try my best to answer.
13th Jan 2019, 6:28 PM
Joery De Loose
Joery De Loose - avatar
0
Edited the question and uploaded my program...pls look into it
12th Jan 2019, 3:03 PM
Mystic77
Mystic77 - avatar
0
Thanks a lot Joery De Loose!! It was what i wanted.I just want to ask more question...do we need to declare a function which returns an array as type[] function_name()? (our teacher didn't go deep into this concept..so....if u dont mind).
12th Jan 2019, 5:18 PM
Mystic77
Mystic77 - avatar
0
yeah just one more question now...what if i wanted to make function which only takes one row of a jagged array and perform some random task..how will the function declaration and te function call statement look like?
13th Jan 2019, 2:45 AM
Mystic77
Mystic77 - avatar
0
Oh okay...thanks a lot buddy..Ur help really means a lot to me :')
13th Jan 2019, 5:51 PM
Mystic77
Mystic77 - avatar