+ 1

how to convert following c code to java

guys how to code following c code in java please help me. int *arr = NULL; int n= 0 ; char ch; arr = (int*)malloc(sizeof(int)); printf("Enter the elements:"); while (scanf("%d%c",&arr[n],&ch)>0 && ch!='\n') { n++; arr= (int*) realloc(arr,(n+1)*sizeof(int)); }

25th Mar 2021, 2:54 PM
Karthikeyan
Karthikeyan - avatar
12 Réponses
25th Mar 2021, 3:46 PM
Odyel
Odyel - avatar
+ 5
Karthikeyan as you know, A POINTER IS JUST THE ADDRESS OF SOME location in memory. As Java has no pointer data types, it is impossible to use pointers in Java, you must to use collection or dynamic array Please read https://www.oracle.com/java/technologies/simple-familiar.html
25th Mar 2021, 3:54 PM
hossein B
hossein B - avatar
+ 4
Karthikeyan Collection is a best option. There is no other option.
25th Mar 2021, 3:43 PM
A͢J
A͢J - avatar
+ 3
array is mutable but size is fixed in Java
25th Mar 2021, 4:23 PM
zemiak
+ 2
This is the goal of your application It uses dynamic memory The user adds data to the dynamic memory as long as he/she does not press Enter You must use List List<Integer> list=new ArrayList<Integer>();
25th Mar 2021, 3:32 PM
hossein B
hossein B - avatar
+ 2
Karthikeyan Or you can use StringBuffer to insert data.
25th Mar 2021, 3:45 PM
A͢J
A͢J - avatar
+ 2
Martin Taylor wow thanks for the kind words, however there is something to make clear: I would never personally make a dynamic array this way. What Karthikeyan ask was for a translation, and I did as close as a direct translation as I could. This is not even close to how I would make a dynamic array. I'd just use a linked list or an array list.
25th Mar 2021, 5:25 PM
Odyel
Odyel - avatar
+ 2
Martin Taylor I'm not mad, just had to make sure that people know that my coding isn't actually *that* bad.
25th Mar 2021, 6:29 PM
Odyel
Odyel - avatar
+ 2
BTW this is valid input for this c code 1a2b3c4
25th Mar 2021, 8:11 PM
zemiak
+ 1
Can you please tell me what the code does? I am not from a C background so it's very hard to understand the code .
25th Mar 2021, 3:10 PM
Soumik
Soumik - avatar
0
Dynamically allocating size of an array and assigning values to array until user inputs number
25th Mar 2021, 3:29 PM
Karthikeyan
Karthikeyan - avatar
0
Thankyou guys, I asked this question because in my coding interview they give languages c and c++ only but I am not good at c and c++ so I asked them for java so they give me chance to do in java with some restrictions don't use collections and string and predefined methods and most of the questions are in array bassed that's why I asked is there any possibilities in java get inputs dynamically. Thanks for all
27th Mar 2021, 4:36 AM
Karthikeyan
Karthikeyan - avatar