Hi all,
hope u guys r fine.

i have a question about array.im using double array,but seems cant support bigger size and im planning to change it to arraylist.but the problem is, can i update the data in arraylist?

int value[]={9,8,9,7,9,7};
int[][] myTupple = new int[count][value.length];
repetition =1;
for(int i=value.length-1;i>=0;i--)
	{

	 if(s.charAt(i)=='1')
		 {
			int currentTupple=0;
			while(currentTupple < count)
			   {
				for(int j=1;j<=value[i];j++)
					{

						for(int k=0;k<repetition;k++)
							{
		                                           myTupple[currentTupple][i]=j;
										        currentTupple++;

							  }
					 }
			 }
			 repetition *= value[i];//****
		 }

im using nested for loop,thats why in d beginning i used double array,but seems problem with double array,so plan to change to arraylist.

anything that i can do?if i can use arraylist, how it works?like double array i can see its like a coodinate (column.row)

Recommended Answers

All 4 Replies

To keep your two dimensional indexing scheme You could have an arraylist that contains arraylists. Dimension one would be into the outer arraylist. A get() there would return the inner arraylist. Dimension two would be into that arraylist.

"can't support bigger size" - you mean, the array doesn't resize dynamically?

The arraylist is one good solution, but you can make a method to increase your array size when it gets too big. That's a pretty simple thing to do. Your method would simply declare a new array of the new size, and copy the elements of the old array into the new one, and then return the new one, which you can assign into the old one's place. The usual received wisdom is that you should double the array each time you grow it, to minimize the amount of time you spend copying one array over to the next.
The advantage of this in your present situation is that you could keep your code pretty much as it is, and just add this method. The disadvantage is you need to do a bit of bookkeeping, to know when you need to double the array (ie, when your last element is approaching the end of the array). This isn't such a hardship, since you need to keep track of where you are in the array at any time in any case, but you need to be sure that you keep track of that each time you add to the array.

Just a suggestion. If it suits your needs, give it a shot.

Member Avatar for coil

It depends on your needs. Use arrays for storing items when you know how many storage spaces you need, and ArrayLists for when you don't.

Jon's method will probably work for one or two modifications, but it might not be the best approach if you're continually copying and creating new arrays.

In response to your original question, yes, you can update data in ArrayLists. Use the set([index], [element]) method.


Jon's method will probably work for one or two modifications, but it might not be the best approach if you're continually copying and creating new arrays.

That's why you use the doubling rule. The array grows to the right scale and stays there. If you're continually copying, then your array's rate of growth is exponential, and you have bigger issues than the time cost of copying the array. :)

The real problem is that this takes you on a track towards rewriting the ArrayList, but that's a good thing if you're doing a homework assignment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.