1

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

Durgaprasad
  • 157
  • 2
  • 6
  • 19

5 Answers5

4

I think the perfect bet of your requirement is ArrayList.

Resizable-array implementation of the List interface.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Sorry i don't want use ArrayList – Durgaprasad Jun 11 '13 at 07:05
  • bcoz my boss is said to me implement this way only since it is assignment give me any suggestion – Durgaprasad Jun 11 '13 at 07:20
  • 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 08: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 08:13
  • 1
    Yeah,It is like using steps ,when you have lift :). – Suresh Atta Jun 11 '13 at 08:14
  • 1
    May be your boss is testing you that weather you are aware of arraylist or not :) – Suresh Atta Jun 11 '13 at 08:17
1

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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

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);
DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

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.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Using java.util.Arrays class it is possible. Please refer below link for answer.

How to increase string array size automatically or dynamically

Community
  • 1
  • 1
UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92