I'm trying to make a Connect 4 game and I'm getting it to work, but I think I'm doing it in a very long and hard way. I think that array were created to simplify code like this, but I'm not sure how to use an array for this occasion. Any help is appreciated. Thanks.

Here is the html file for my project and the source code is in the attachment.

<HTML>
<BODY>
<APPLET CODE=Connect4.class WIDTH=1000 HEIGHT=600>
</APPLET>
</BODY>
</HTML>

Syntax for an array is simple--

ClassName_[] variableName = new ClassName_[SIZE_OF_ARRAY];

//or

ClassName_ variableName[] = new ClassName_[SIZE_OF_ARRAY];

Where ClassName_ is a valid class name, variableName is the name you give the array and SIZE_OF_ARRAY is the amount of objects you want in the array.

an example...

String[] names = new String[9];//an array of Strings, can hold up to 9 strings from indices
                                              //0-to-8

Arrays begin at indice zero, so calling your first element is in the syntax--

String myName = names[0];//storing the value in names[0] to myName;

Now myName has whatever value that was in names[0].

But in order for there to be a valid value in names[0] the array has to be populated (initialized) first--

for(int i = 0; i < names.length; i++)//arrays have a public variable named length that
{                                                  //is the value of their capacity
        names[i] = new String("" + i);//giving each name the name of the int value
}

or when you create the array you can initialize it--

String[] names = {"Mark", "Jason", "Valerie"};//an array of 3 strings from indice 0-to-2

Two words of warning!

First off, you can't initialize an array after the line of declaration.
Secondly you cannot change the size of the array once it has been created.

Once you get adjusted with using Arrays, you'll most likely want to use a more lenient class like ArrayList or Vector... but for simplicity stick to arrays!

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.