2
 jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null},
        {null, null, null},
          },
    new String [] {
        class2.columnName[0],class2.columnName[1],class2.columnName[2]                    
    }
));

Column names array In class2:

static String[] columnName={"Name", "data" , "data2"};

I want to set 3 Columns names in JTableJTable from String[]String[] Columns. I did this by using array index number by coded hard: but if I have hundreds of values in the array then how can Ii set jtable column names by using array values without using hardcoded array index numbers but by using any other less typing way

2 Answers2

3

If the array is never modified by any means and you want to use the whole array, then you can use the array reference directly:

jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {Readsheet.Stringvalue, Readsheet.Stringvalue, Readsheet.Stringvalue},
        {Readsheet.Stringvalue, Readsheet.Stringvalue, Readsheet.Stringvalue},

    },
    Writesheet.Columns // <-- Array reference
));

If the array can be modified, you can create a copy of the whole array. There are many ways to do it, my personal preference is Arrays.copyOf:

    Arrays.copyOf(Writesheet.Columns, Writesheet.Columns.length) // <-- Copy of the entire array

If you only need a contiguous part of the array, you can create a copy of an array slice:

    Arrays.copyOfRange(Writesheet.Columns, 0, 3) // <-- Copy of a part of the array, indexes from 0 to 2

Finally, if you need to extract elements by individual non-contiguous indexes and don't want to type Writesheet.Columns[...] many times, you can use a stream of indexes and extract the values using Stream.map():

    IntStream.of(0, 1, 2) // <-- Indexes here
        .mapToObj(i -> Writesheet.Columns[i]).toArray()

Update: to join the resulting array to a string using , as separator:

    IntStream.of(0, 1, 2) // <-- Indexes here
        .mapToObj(i -> Writesheet.Columns[i]).collect(Collectors.joining(","))
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • Java doesn't have any special language constructs for this. Basically, you have the options above, plus you can write a static utility method which takes a strings array and an array of indexes, and returns the extracted values, i.e. `public static String[] getByIndexes(String[] array, Integer... indexes) { return Arrays.stream(indexes).map(i -> array[i]).toArray(); }` – Alex Shesterov Sep 24 '18 at 13:03
  • In your original question you wanted an array, not a joined string. If you need the string, you can use the stream and `.collect(joining(","))`, see the last update. – Alex Shesterov Sep 24 '18 at 13:26
1
new String[]{Writesheet.Columns[0], Writesheet.Columns[1], Writesheet.Columns[2]}

can be replaced by a reference to the array

Writesheet.Columns

while

{Readsheet.Stringvalue, Readsheet.Stringvalue, Readsheet.Stringvalue}

can be replaced with

Stream.generate(() -> Readsheet.Stringvalue).limit(3).toArray(String[]::new)

which can be generalised into a method

public static <T> T[] generateArrayFromElement(T element, int size, IntFunction<T[]> arrayGeneratorFunction) {
    return Stream.generate(() -> element)
                 .limit(size)
                 .toArray(arrayGeneratorFunction);
}
...
generateArrayFromElement(Readsheet.Stringvalue, 3, String[]::new);

(we generate an infinite stream of a single element, limit to the required size, and collect into an array)

or

public static <T> T[] generateArrayFromElement2(T element, T[] array) {
    Arrays.fill(array, element);
    return array;
}
...
generateArrayFromElement2(Readsheet.Stringvalue, new String[3]);

(we fill the given array with a single element and return it)

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 1
    @EZRA, `limit(size)` makes a generator of `Stream.generate` fill a stream with generated elements (here, the generator is `() -> element`, which gives `element` every time we ask) – Andrew Tobilko Sep 24 '18 at 16:36
  • 1
    @EZRA, `toArray(IntFunction)` is needed to produce a collection of the correct types (here, the `IntFunction` is `n -> new String[n]`, or shortly `String[]::new`) – Andrew Tobilko Sep 24 '18 at 16:37