Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How the array size is auto increment in java program my requirement is "create a 2 dimensional array sno, in the first dimension name, age in the second dimension so if i pass sds, 25 it should be added to the array [0][sds,25] the array should keep incrementing" my code is like this:

void values() throws IOException{for ( int row = i; row < len; row++ ){
            for ( int column = 1; column <= 1; column++ ){
                arrayValues[row][0] = String.valueOf(row+1);
                System.out.print("Enter "+(row+1)+" Name: ");
                String name = br.readLine();
                System.out.print("Enter "+(row+1)+" age: ");
                int age = Integer.parseInt(br.readLine());
                arrayValues[row][column]= name+","+age;
            }
void incrementSize() throws IOException{String[][] newArray = new String[arrayValues.length][];
    System.out.println(newArray.length);
String[][] t = Arrays.copyOf(arrayValues, newArray.length);

After this how can done my code Please help me

share|improve this question
3  
Sounds like you should be using a different data structure. Have a look at arraylist. –  SBI Jun 11 '13 at 6:52
 
No boss is said to me implement this way only –  Durgaprasad Jun 11 '13 at 7:22
1  
May be he is testing you that weather you are aware of arraylist or not :) –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 11 '13 at 8:15
add comment

4 Answers

I think the perfect bet of your requirement is ArrayList.

Resizable-array implementation of the List interface.

share|improve this answer
1  
What is the reason ?? –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 11 '13 at 7:07
1  
If you want to use arrays for resizable arrays you'll end up recreating what arraylist does "under the hood" anyway (probably less efficiently). I really would check you boss doesnt want arraylists –  Richard Tingle Jun 11 '13 at 8:10
1  
P.s. why are you throwing away all the object orientated nature and going with a 2D array rather than a 1D array of Person objects (each with a name and age field). As you're doing it now you're holding age in a String, that makes me very sad –  Richard Tingle Jun 11 '13 at 8:13
1  
Yeah,It is like using steps ,when you have lift :). –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 11 '13 at 8:14
1  
May be your boss is testing you that weather you are aware of arraylist or not :) –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 11 '13 at 8:17
show 2 more comments

You have to create a new array in order to make the size bigger, there is no dynamic way to increase an existing array.

Here is a way you can create a new array to increase the size.

int[] a = new int[5];
// fill a
int[] b = Arrays.copyOf(a, 10);
share|improve this answer
 
for single is ok, but how is 2-D and if i can do this way that shows null pointer wxception –  Durgaprasad Jun 11 '13 at 7:07
add comment

Use List for this requirement. You can use a ArrayList<ArrayList> in place of 2-dim array. ArrayList is Resizable-array implementation of the List interface.

An array is a container object that holds a fixed number of values of a single type.

share|improve this answer
add comment

Sounds like you are looking a dynamically growing data structure. you can use implementation of list interface which is in java collection frame work. you can use ArrayList or Vectors.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.