Problems generating names for instancevariables in a vector

I

iMohed

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created. I tried something like the algorithm below but
it's not working. Any help or suggestions would be welcome and thanks
in advance.

public class Board {

public int numSquares;
Square[] sq = {};

public void mksquares(int k){

for (int i = 0; i < k; i++){
Square genName("Sq", i) = new Square(); // "here is
the rub"
}

public String genName(String s, int i){
String name = new String();
name.concat(s+i);
return name;
}
}

public class Square {

int p;
int value;
Color c;
}
}

Mohamed Haidar.
 
J

Joshua Cranmer

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created. I tried something like the algorithm below but
it's not working. Any help or suggestions would be welcome and thanks
in advance.

You could use a list or even a regular array:

public class Board {
private Square[] squares;

public void mksquares(int k) {
squares = new Square[k];
for (int i = 0; i < k; i++)
squares = new Square();
}
}
 
L

lord.zoltar

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created. I tried something like the algorithm below but
it's not working. Any help or suggestions would be welcome and thanks
in advance.

   public class Board {

        public int numSquares;
        Square[] sq = {};

        public void mksquares(int k){

            for (int i = 0; i < k; i++){
                Square genName("Sq", i) = new Square();   // "here is
the rub"
            }

            public String genName(String s, int i){
                String name = new String();
                name.concat(s+i);
                return name;
                }
        }

        public class Square {

            int p;
            int value;
            Color c;
        }
    }

  Mohamed Haidar.

I don't know what you're trying to do so I'm not even sure you NEED to
give the objects specific names... but if you really Really REALLY
think you do, you could try:


public class Square {

int p;
int value;
Color c;
private String name;

public Square(String s)
{
name = s;
}

public String getName()
{
return name;
}
}

and then this:
Square genName("Sq", i) = new Square();

could become this:
sq = new Square("Sq"+i);

(I haven't actually tried compiling this but it should give you a good
idea)
 
I

iMohed

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created. I tried something like the algorithm below but
it's not working. Any help or suggestions would be welcome and thanks
in advance.

You could use a list or even a regular array:

public class Board {
   private Square[] squares;

   public void mksquares(int k) {
     squares = new Square[k];
     for (int i = 0; i < k; i++)
       squares = new Square();
   }

}


But would my Squares in the array have names or doesn't they recure
any ??
 
L

lord.zoltar

But would my Squares in the array have names or doesn't they recure
any ??

So the question becomes: Do you need the objects to have names? What
would you do with the names? What purpose would they serve you? Can
you find a way to do what you're doing WITHOUT names?
 
J

Joshua Cranmer

But would my Squares in the array have names or doesn't they recure
any ??

What do you mean by `name'? You can access any individual element via
squares, but if you need to get a name for a particular Square, you
would probably be best storing that information in the Square class itself.
 
L

Lew

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created. I tried something like the algorithm below but
it's not working. Any help or suggestions would be welcome and thanks
in advance.

   public class Board {

        public int numSquares;
        Square[] sq = {};

        public void mksquares(int k){

The method name does not adhere to the Java naming conventions, which
call for camel case in variable and method names, starting with a
lower-case letter. Also, there is no good reason to be so terse with
the name.
            for (int i = 0; i < k; i++){
                Square genName("Sq", i) = new Square();   // "here is
the rub"

This is not legal Java syntax. A variable name must follow the rules,
which do not include parentheses, quotes, commas or spaces.

You can associate a name with an object by including it as an
attribute of the object, or by using a map such as 'Map <String,
Square>', that maps the name to the object. In fact, maps are a
standard way to implement associative arrays in Java.
            }

Try something like this (no more complete than the OP code):

private static final String PREFIX = "square";
public Map <String, Square> makeSquares( int k )
{
Map <String, Square> squares = new HashMap <String, Square> ();
for ( int i = 0; i < k; i++ )
{
squares.put( PREFIX + i, new Square() );
}
return squares;
}
 
M

Mark Space

squares = new Square[k];
for (int i = 0; i < k; i++)
squares = new Square();
}

But would my Squares in the array have names or doesn't they recure
any ??


Short answer: they don't require a name.

If you use an array, you access the object by number, not name. That's
what Joshua showed you. After filling squares[] with the for-loop, you
can access an individual square like this:

Square sq = squares[0]; // the first Square

or any number up to the size of the array.

If you truly don't know the number of Squares, you can use an ArrayList,
which can hold any number of objects, up to the size of memory.

However, if this is a school assignment (as I am beginning to expect) I
think your teacher probably wants you to use a new array statement:

squares = new Squares[42];

This creates the array, with a fixed number of elements. You can put
anything in place of the 42, including variables and mathematical
expressions. Note Joshua uses 'k' instead of 42. It's the same idea
though.
 
L

Lew

Mark said:
However, if this is a school assignment (as I am beginning to expect) I
think your teacher probably wants you to use a new array statement:

   squares = new Squares[42];

This creates the array, with a fixed number of elements.  You can put
anything in place of the 42, including variables and mathematical
expressions.  Note Joshua uses 'k' instead of 42.  It's the same idea
though.

Be aware that the 'new' expression shown does not create 42 new
'Square' instances, but 42 pointers for 'Square', each initialized to
'null'. You will still need to assign a 'new Square()' to each
pointer that should not be 'null'.
 
T

Tom Anderson

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created.

This is one of the most common fundamental misconceptionsi hear.

I wonder - would it be possible to make a language that worked this way?
How would it work? Would it be easier for people to understand?

tom
 
M

Mark Space

Tom said:
I wonder - would it be possible to make a language that worked this way?
How would it work? Would it be easier for people to understand?


for( i = 0; i < LIMIT; i++ )
hasMap.put( "name" + i, new Object() );

Not really, imo. You still need to understand indexes, counting, method
calls, setters, getters, execution order, constructors, order of
operations, etc.
 
R

Ratnesh Maurya

Hello guys. I wanted to wright something that filled a vector with
objects of the same type. Sins I don't know how many objects there is
going to be beforehand I wanted some way to generate names for them
when they are created. I tried something like the algorithm below but
it's not working. Any help or suggestions would be welcome and thanks
in advance.
   public class Board {
        public int numSquares;
        Square[] sq = {};
        public void mksquares(int k){

The method name does not adhere to the Java naming conventions, which
call for camel case in variable and method names, starting with a
lower-case letter.  Also, there is no good reason to be so terse with
the name.


            for (int i = 0; i < k; i++){
                Square genName("Sq", i) = new Square();   // "here is
the rub"

This is not legal Java syntax.  A variable name must follow the rules,
which do not include parentheses, quotes, commas or spaces.

You can associate a name with an object by including it as an
attribute of the object, or by using a map such as 'Map <String,
Square>', that maps the name to the object.  In fact, maps are a
standard way to implement associative arrays in Java.
            }

Try something like this (no more complete than the OP code):

 private static final String PREFIX = "square";
 public Map <String, Square> makeSquares( int k )
 {
  Map <String, Square> squares = new HashMap <String, Square> ();
  for ( int i = 0; i < k; i++ )
  {
    squares.put( PREFIX + i, new Square() );
  }
  return squares;
 }

I Agree with Lew and the map implementation suggested here is best for
this scenario :)

Regards,
-Ratnesh
S7 Software
 
T

Tom Anderson

for( i = 0; i < LIMIT; i++ )
hasMap.put( "name" + i, new Object() );

Nonono. Something like:

for $i = 1 to 10 {
$variableName = "name" + $i;
$$variableName = new Object();
}

Which is an imitation of some genuine syntax in perl.

You can probably do it in bash, something like:

for i in $(seq 10)
do
varname="name$i"
eval "$varname=somevalue" # this won't actually work
done

I'm pretty sure i've done something like that in the past. Can't remember
how!

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top