Jump to content

Hello, I could really use someones help on this... I have to create a program where a user can infinity enter values (so not a static array) and then when send user is done entering arrays  it adds, averages, etc... the problem i cant figure out to to re-size the array on every input. also I would need this done using basic java beginner code like basic for loops. If anyone could help me, or at least point me in the right direction i would greatly appreciate it.

Link to comment
https://linustechtips.com/topic/591446-need-help-with-arrays/
Share on other sites

Link to post
Share on other sites

To re size an array, you:

  1. Make a new array which is the size you need (probably one larger than the original)
  2. Transfer each element from the original array to the new array

Note that usually, a 'list' is used in Java ('list' is an interface, 'Arraylist' is one of the most common implementations) instead of an array.

An arraylist automatically adjusts it size, and since its an object, has various mehtods that make it easier to work with than an array

i7 4790k | MSI Z97S SLI Krait Edition | G.Skill Ripjaws X 16 GB | Samsung 850 EVO 500 GB | 2x Seagate Barracuda 2TB | MSI GTX 970 Twin Frozr V | Fractal Design R4 | EVGA 650W

A gaming PC for your budget: $800 - $1000 - $1500 - $1800 - $2600 - $9001

Remember to quote people if you want them to see your reply!

Link to comment
https://linustechtips.com/topic/591446-need-help-with-arrays/#findComment-7696863
Share on other sites

Link to post
Share on other sites

5 hours ago, Darr1en13 said:

Hello, I could really use someones help on this... I have to create a program where a user can infinity enter values (so not a static array) and then when send user is done entering arrays  it adds, averages, etc... the problem i cant figure out to to re-size the array on every input. also I would need this done using basic java beginner code like basic for loops. If anyone could help me, or at least point me in the right direction i would greatly appreciate it.

Some code for what @HPWebcamAble is talking about:

ArrayList<Datatype> arraylistofdata = new ArrayList<Datatype>();
arraylistofdata.add(Instance of Datatype);
arraylistofdata.get(index of item);

Those are the most common things you will likely use, the first declares an arraylist of a data type, the second adds an element to the list, and the third one gets the element at that index in the arraylist. Happy coding xD

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
https://linustechtips.com/topic/591446-need-help-with-arrays/#findComment-7697929
Share on other sites

Link to post
Share on other sites

personally i would would pick a big number, i highly doubt a user will type in that many. less than a 100 i would say.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/591446-need-help-with-arrays/#findComment-7700010
Share on other sites

Link to post
Share on other sites

13 hours ago, dannytech357 said:

Some code for what @HPWebcamAble is talking about:


ArrayList<Datatype> arraylistofdata = new ArrayList<Datatype>();
arraylistofdata.add(Instance of Datatype);
arraylistofdata.get(index of item);

Those are the most common things you will likely use, the first declares an arraylist of a data type, the second adds an element to the list, and the third one gets the element at that index in the arraylist. Happy coding xD

I would say ArrayList like Dannytech357.

 

Take a look here: http://tutorials.jenkov.com/java-collections/list.html

It's the basic on how to use it.

Link to comment
https://linustechtips.com/topic/591446-need-help-with-arrays/#findComment-7700539
Share on other sites

Link to post
Share on other sites

Tbh, you can make just a really big array and use that one.

ifstream in(/*...*/);
int v[10000],n=0;
while(in>>v[n++])
{
//etc.
}

Or you could use a dynamic data structure,like a linked list. Working with pointers is not that fast though, so a dynamic vector would probably be faster.

vector<int> v;
int temp;
while(in>>temp)
  v.push_back(temp);

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/591446-need-help-with-arrays/#findComment-7701221
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×