java


Is it possible to make an array with dynamic memory using while loop without asking the size of memory from user?


I'm trying to make a programme that it doesn't ask user the value i, where i being the number of elements of an array, ( int[] someArray = new int[i]).
Two problems i'm facing , first how the programme auto locates memory size , and second one is, facing trouble with different types of data( i know this one is trivial but just could't put together the logic).
Basically my programme structure is like below:
Scanner input = new Scanner(System.in);
int[] someArray;
int element;
String order;
while(!("done").equals(order=input.nextLine())){
if(some integer){
//set the user input as the value of array element, and change the pointer to the next element
}
if(some string other than "done"){
System.out.println();
//continues the loop
}
}
You can just use an already existing growing collection, such as ArrayList, LinkedList, etc. You add as many elements as you want and they take care of dynamically allocating the necessary space.
This is called dynamic resizing and has the amortized complexity O(n).
The main idea is to double the array size every time your array gets full.
For implementation details I would take a look here.
PS: don't forget to mark some answers for your questions as SOLVED, as it seems that you never do that.

Related Links

Round time by seconds
Two versions of the same class (1 static and one non static)
Jackson won't find my module
Packge level Namespace JAXB
altbeacon with service class- android
Error while running allure command line generate command
Overriding or Listener
Generate Sudoku 9x9 from 3x3 LatinSquare
Efficient way to find no of occurances of some property value in object as value of hashmap
Split XML based on size using Java
Sql Groupby with mutliple column Need Store procedure Java Spring Hibernate
How can I solve the error : 'jar' is not recognized as an internal or external command, operable program or batch file?
PCI DSS Compliant – How can I truncation of credit card number using Java.
Change table header color using boxable with pdfbox
Get the JSON response instead of text/html
log4j logger updating log file but not the file system

Categories

HOME
compiler-construction
xamarin
asp.net-core
openmp
google-api-php-client
appx
syntax
android-4.4-kitkat
angular-ui-bootstrap
autotools
node-pdfkit
spring-xd
apache-cayenne
size
after-effects
oxyplot
web-sql
pass-by-reference
crosstab
gitignore
instant-messaging
fabric8
siesta-swift
vsts-build-task
xmlreader
atl
jmonkeyengine
ios5
event-driven
dotcover
clean-architecture
idisposable
magma
android-cursor
qtwebview
mcafee
minimization
pycaffe
pintos
objective-c-swift-bridge
medium.com
deadbolt-2
log4c
storekit
javax.sound.midi
ora-00900
libressl
iis-arr
key-management
coldbox
phalanger
qcodo
preferences
eol
uv-mapping
cdc
web2py-modules
odata4j
factory-method
distutils
path-separator
onsubmit
ticoredatasync
ecl
imac
.nettiers
icanhaz.js
mirah
firefox-5
libs
javap
ajax-forms
uiq3

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App