+ 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)); }
12 Réponses
+ 2
+ 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
+ 4
Karthikeyan
Collection is a best option. There is no other option.
+ 3
array is mutable but size is fixed in Java
+ 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>();
+ 2
Karthikeyan
Or you can use StringBuffer to insert data.
+ 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.
+ 2
Martin Taylor I'm not mad, just had to make sure that people know that my coding isn't actually *that* bad.
+ 2
BTW this is valid input for this c code
1a2b3c4
+ 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 .
0
Dynamically allocating size of an array and assigning values to array until user inputs number
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