Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create an array dynamically

Posted on 2014-04-20
6
Medium Priority
?
182 Views
Last Modified: 2014-04-21
My Java code will prompt users to input some data, each piece of this data will need to have an array attached to it. Problem is I will not be privy to how many pieces of data will be entered, it could be just one or 10,000. So I won't know how many arrays or array lists I will need. I want to create the arrays as I need them. In other words I do not want to declare a bunch of arrays prior to knowing how many I need.
0
Comment
Question by:CrazyOne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 40011827
You can create arrays or array lists dynamically.

Here's an example of having a list of arrays, where the arrays have a random size from 0-99 elements.  In your example you'd probably want to decide on this size based on what the user inputs instead.

Then it collects up the arrays in a list, so you can find them again later.

		List<Integer[]> listOfArrays = new ArrayList<Integer[]>() ;

		boolean done = false ;

		while (!done) {
			int arraySize = (int)Math.round(Math.random() * 100) ;
			Integer[] myNewArray = new Integer[arraySize] ;
			listOfArrays.add(myNewArray) ;

			if (listOfArrays.size() > 10)
				done = true ;
		}

Open in new window


Does that make sense?

Doug
0
 
LVL 44

Author Comment

by:CrazyOne
ID: 40011837
Um, thanks. I am not looking for sizing the array, I am looking to create arrays on the fly. For instances lets say per the users input I will need twenty arrays and another may need 40 arrays. I would like to be able to create the arrays dynamically in code. Something like:

   for(int i = 0; i < HowManyArrays; i++) {
      int Di = new int[size];
      //where Di means the letter D 
      //and the value of i
   }

Open in new window

0
 
LVL 28

Expert Comment

by:dpearson
ID: 40011859
I am looking to create arrays on the fly

Yes I understand that.  The code I posted shows you how to dynamically create 10 arrays in a loop.

If they are all the same size, then just set the "arraySize" variable to a constant, instead of the random value I posted.

I used a while loop in case you don't know at the start how many arrays to create.  If you know that at the start, then a for loop (like you posted) is simpler.

Doug
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 44

Author Comment

by:CrazyOne
ID: 40011914
OK . Excuse my ignorance it has been 10 years or more since I sat down, and did some real hard-coding for an app, but how do I access the various newly created arrays. In your code there are 10 arrays, lets say I need to add or extract info from the 5th array how do I do it. Thanks

P.S. If you like could I ask this in a separate question, so as to get you more point, since this is an add to my original question.
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40011981
OK here's a somewhat simplified version of my original code - removing the while loop and the varying array size and then looking up the 5th element in the list and then adding an extra element as the 5th element.

               List<Integer[]> listOfArrays = new ArrayList<Integer[]>() ;

		for (int i = 0 ; i < howManyArrays ; i++) {
			int arraySize = 10 ;
			Integer[] myNewArray = new Integer[arraySize] ;
			listOfArrays.add(myNewArray) ;
		}

              // index 4 is the 5th element since we start at 0
               Integer[] fifthArray = listOfArrays.get(4) ; 

               // Add an extra array in at the 5th position (i.e. index 4)               
               Integer[] addArray = new Integer[10] ;
               listOfArrays.add(4, extraArray) ;

Open in new window

0
 
LVL 44

Author Closing Comment

by:CrazyOne
ID: 40012776
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question