programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Issue in appending and stroring the elements in 2d int array in java

Greenhorn
Posts: 7
Hi,

I have an issue in adding an array elements and store it into a 2d int array.

An example,

I have a 2d int array(newint2d[][]) which contains 2 elements initially.

0101100000010111101

0011100110111100110

In each iteration will be getting 2 elements.

The looping condition (5times) will produce 10 elements like this randomly at the end of looping statements.

For each iteration i need to add the 2 elements with next 2 and again next 2 like that in 5th iteration i should append the last 2 elements to the 8 elements.

So that i will get 10 elements at final iteration.

And that should store in the 2d int array newint2d[][] in this.

Finally the 2d int array (newint2d[][]) or new 2d int array should contains all the 10 elements stored in it.

For Eg:

0011100000010111101

0101100110111100110 I iteration

0011100000010111101

0101100110111100110 II iteration

0011100000010111101

0101100110111100110 III iteration

0011100110110111101

0101100000011100110

0101100000010111101

0011100110111100110

How to solve this problem?

If there is something i didn't explain well or you need more code to understand the question let me know.

Thanks in advance

Bartender
Posts: 3185
34
Can you show us the code of what you have done so far? This will help us to help you.

Moreover I havent been able to understand your problem statement. Hopefully someone who has understood will be able to explain the problem statement more clearly.

Marshal
Posts: 4269
253
Isn't this just a reworded version of the same question you posted a few days ago?

Bartender
Posts: 10575
66

Gladis Thomas wrote:If there is something i didn't explain well or you need more code to understand the question let me know.

Yes: What are these "numbers"? They would appear to be binary, and contain 19 'digits' each. Is there some significance to that, or is it just coincidental?

I'm also not quite sure why you need a two-dimensional array. Your other thread mentioned a "chromosome" and an associated "number", which looks very much like an index to me (and therefore redundant). If you have a chromosome #3, why not just put its value in element 3 of a one-dimensional array?

Better still: Why don't you back up and tell us what you're trying to do - in English.

So far we have a "solution" (which presumably doesn't work), but I for one have no idea what the problem is.

Winston

Gladis Thomas
Greenhorn
Posts: 7
Hi,

This is not the same thread which i raised before. I tried converting into an 2d int array.

And i have that 2d int array now, but storing the next iteration elements are the problem.

Am doing my project in Genetic Algorithm for Feature Selection.

These 19 digits represent Features in the chromosome(the length of chromosomes will be 19). '1' represent features present and '0' represent features not present(absence).

While doing crossover(one point), will get 2 children.

Those 2 children are below,

0101100000010111101

0011100110111100110

But i need a population to be filled so i am performing crossover operation 5 times so that i can get 10 children, which means my population pool got filled.

The generation of children in crossover is done by randomly. So i cant say only this 10 children i will get all the time.

For each iteration am getting 2 children, its getting overridden in next iteration.

So at 5th final iteration am getting the last iteration children(2).

But i need 10 children to be stored in that 2d int array.

Why 2d array means, now am having the array size of [10][19].

Those 19 bits of 10 chromosomes will not be occupied in 1d array.

And once i got into 2d array of 10 children then i need to find fitness function for all 10 children.

The fitness function written in such a way that it will accept 2d int arrays.

So that we can test our project with many data sets with many features(chromosome length which is of 0's and 1's).

If you still need more details let me know.

Now i want to know how to append and store the 2d int[][] array dynamically.

Can you help me with this?

Thank you.

Winston Gutkowski
Bartender
Posts: 10575
66

Gladis Thomas wrote:Those 19 bits of 10 chromosomes will not be occupied in 1d array.

Sure they will.

A Java int contains 32 bits - more than enough to store 19 bits of information. And furthermore, the bitwise operators that work on them are blisteringly fast.

And 19 random bits can be easily generated with:
Random.nextInt(1 << 19);

If it were me, I think I would simply store these 'features' (or a 'chromosome') as an int, with the rightmost bit being "feature 0" and then counting upwards as you move to the left to "feature #18", but the arrangement is entirely up to you.

Indeed, I think I'd probably wrap it all in a Chromosome class and then just store an array of them - ie:
Chomosome[] choromosomes = new Chomosome[10];

Alternatively, you could store your 19 'features' as a BitSet (←click); but it might be harder to randomize. And even if I did that, I'd still wrap it in a Chromosome class.

Winston

Gladis Thomas
Greenhorn
Posts: 7
Hi,

It is not just 19bits. It is of 10 chromosomes each has 19features (10*19 =190bits).

That is not possible to store rite.

But i need a 2d int array for my project to proceed with because the fitness function will accept 2d int array only.

Is there any solution for me to append and store the 2d int array as i asked?

Thank you.

Winston Gutkowski
Bartender
Posts: 10575
66

Gladis Thomas wrote:It is not just 19bits. It is of 10 chromosomes each has 19features (10*19 =190bits).

So: 10 ints; 1 dimension.

But i need a 2d int array for my project to proceed with because the fitness function will accept 2d int array only.

Ah, now THAT you didn't explain. You would appear to be hamstrung by a crappy design.

Is there any solution for me to append and store the 2d int array as i asked?

Sure there is:
int[10][19]
as you've already worked out.

Each "row" (the first index) will then be an int[] containing a "chromosome" of 19 "features" (each of which is presumably an int containing '0' or '1').

However, it really is an awful design - fiddly and wasteful of space. But if that's what you've been given, I guess that's what you'll have to use.

Personally, I'd still be tempted to write my own Chomosome class, and simply have it do conversions where needed for these horrible "2D arrays" you've been forced to use.

But better still would be for you to show us the code you've written, and where you're running into problems. I still don't quite understand these
0101100000010111101
"numbers" you keep writing. They would appear to be representations of a chromosome, but without the actual code it's difficult to put them (or your problem) into context.

 I should add that we only need to see the bit where you're having problems, and maybe enough other stuff so we can follow what's going on - we don't need a source dump of your entire application.

Winston

Marshal
Posts: 57439
175

Gladis Thomas wrote: . . . I tried converting into an 2d int array. . . .

No, you didn&aps;t because there ain't no such thing as a 2D array in Java®.
All that bit about the numbers representing crossovers (etc) tells me that you are mistaken to try to use numbers. As I said before, where is your Chromosome class?