Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

fadilthrejk

[java] A new philosophy of my program has come. And I have a ? on Labels outputting arrays

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a method to convert the whole contents of an array to a string so I can output it to a label? Spread the rusty bleen ointment on the blue-red fromato with your thimble carver [edited by - fadilthrejk on March 31, 2003 12:27:50 PM]

Share this post


Link to post
Share on other sites
Advertisement
btw, here is the code I have for my game so far, and I feel much better on the way it's going than before.

I have two classes right now, Deck and Engine.

Engine:

    
package duel_monsters;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 0.1
*/

public class Engine extends Applet implements ActionListener
{
Deck deck1 = new Deck(40);//YAY!!! The first class I ever made, my step into OOP!!!!
Label test = new Label(deck1.deckCreate(40));

public void Stage1()//get a more descriptive method name eventually
{

}//end Stage1

public void init()
{

}//end init

public void actionPerformed(ActionEvent e)
{

}
}//end Engine


Deck:

package duel_monsters;

/**
* <p>Title: Duel Monsters </p>
* <p>Copyright: Copyright (c) 2003</p>
* @author changed to protect the n00b (moi)
* @version 0.1
*/


public class Deck
{
java.util.Random generator = new java.util.Random();

int DeckSize;
int[] arrDeck;

public Deck(int dS)
{
DeckSize = dS;
}//end Deck


public int[] deckCreate(int dS)
{
for (int i = 0; i < DeckSize; i++)
{
arrDeck[i] = generator.nextInt(100);
}//end for

return arrDeck;
}//end deckCreate


}//end Deck



I am trying to think of new functions to add to my Deck class, to add functionality to it, not just that one function. Any suggestions?
Also, how would I make it so on a certain event, the screen goes to a certain layout, like one for the playing area, one for the battle screen, etc.? I think I would do a simple if-else or nested if-else loop, because I don't plan on having more than 3 layouts, but I'm not sure if there is a better way or not.

[edited by - fadilthrejk on March 31, 2003 12:51:12 PM]

Share this post


Link to post
Share on other sites
something like this should work:


    
String MyClass:arrayToString(Object array[])
{
String string = "";
for (int i=0;i<array.length;i++)
{
string += array[i].toString() + "
\n";
}
return string;
}


I didn't test the above method but it should work. It also assumes you implemented the toString() function for all the objects in the array.

Hope that helps.


--- COMPUTABILITY ---

[edited by - Computability on March 31, 2003 12:50:08 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Forgot the closing bracket after the return.


public String DeckIntString(int dS){
String strDeckInt = "";
for (int i=0; i<dS; i++){ strDeckInt += arrDeck.toString() + "\n";
}//end for
return strDeckInt;
}

Share this post


Link to post
Share on other sites
I just cleared the folder all that stuff was in, so I could start from scratch, knowing what I am doing. Hopefully I'll figure that out.

[edited by - fadilthrejk on March 31, 2003 1:49:53 PM]

Share this post


Link to post
Share on other sites
Hi,

if you''ll ever concatenate strings in a loop, DO NOT use the "+=" operator. Compiler will translate the line:

someString = someString + ", ";
// someString += ", "; <-- this is the same bad use

into:

someString = new StringBuffer(someString).append(", ").toString();

The point is doing this once is no problem, but iterating over this statement 100 times means, that the JVM will create 99 StringBuffer objects more, than you actually need.

Just try it out yourself and write a test program that does some string concatenation via the "+=" operator in one loop and StringBuffer operations in the other. Iterate some 10k times and measure the time it takes to complete. You''ll be quite surprised.

P.S.: The correct stringification procedure for an array would rather be then:


public static String stringifyArray( Object [] theArray ) {
if (theArray == null) throw new IllegalArgumentException("The parameter must not be null!");
StringBuffer result = new StringBuffer(theArray.length > 0 ? String.valueOf(theArray[0]) : "");
for( int i = 1; i < theArray.length; i++ ) {
result.append(String.valueOf(theArray));
}
return result.toString();
}


maybe this is what you search for?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!