hi i have to create a program that deals cards and shuffles them. when i pull a card i wanted to put it into a new array and remove it from the first one. im new so dont be surprised if i make obvious mistakes
this is an example of what im trying to do
using
System;using
System.Collections;using
System.Text;namespace
ConsoleApplication1{
class Program:ArrayList{
static void Main(string[] args){
int i = 0; Class1[] abc = new Class1[52];abc[i] =
new Class1("string"); //i have tried 2 things here to test itabc[i].Clear();
// and ive tried abc[i].removeAt(i) since abc.removeAt does nothing if (abc[i] == null) Console.Out.WriteLine("null"); else{
Console.Out.WriteLine("not null"); Console.Out.WriteLine(abc[i]);}
Console.In.Read();}
}
}
using
System;using
System.Collections;using
System.Text;namespace
ConsoleApplication1{
public class Class1:ArrayList{
string STR; public Class1(string str){
STR = str;
}
public override string ToString(){
return STR;}
}
}
can anyone plz tell me what im doing wrong?
AlanPosted Oct 28, 2007, 12:14 PM
You're doing a couple of things wrong here, Adam:
1. You don't want to inherit from ArrayList which is what both the Program and Class1 classes are doing - you just want to store objects in an ArrayList. In fact, I'm not sure you want to be using ArrayLists at all because, as there are 52 cards in the pack, you know in advance the maximum number of elements there are going to be in the array.
2. An 'ordinary ' array such as 'abc', once created, always maintains its size - you cannot add extra elements or remove elements from it. To simulate removal of a particular element, you simply set it to null.
So, I would suggest you change the code to the following:
using System;
using
System.Collections;using
System.Text;namespace
ConsoleApplication1{
class Program // removed ArrayList as base class{
static void Main(string[] args){
int i = 0; Class1[] abc = new Class1[52];Class1[] def = new Class1[52]; // new array to hold cards dealt out
abc[i] = new Class1("string");
def[i] = abc[i]; // copy first element of abc to def
abc[i] = null; // 'remove' first element from abc by setting it to null
if (abc[i] == null) Console.Out.WriteLine("null"); else{
Console.Out.WriteLine("not null"); Console.Out.WriteLine(abc[i]);}
Console.In.Read();}
}
}
using
System;using
System.Collections;using
System.Text;namespace
ConsoleApplication1{
public class Class1 // removed ArrayList as base class{
string STR; public Class1(string str){
STR = str;
}
public override string ToString(){
return STR;}
}
}
Incidentally, if you did want to use ArrayLists instead of ordinary arrays, then the code for the Main() method should be changed to the following:
static void Main(string[] args)
{
int i = 0; ArrayList abc = new ArrayList(52); // sets capacity to 52ArrayList def = new ArrayList(52); // new arraylist to hold cards dealt out
abc.Add(
new Class1("string")); // add first element to abcdef.Add( abc[i]); // copy first element of abc to def
abc.RemoveAt(i); // remove first element from abc which now is empty (i.e. Count property is zero)
if (abc.Count == 0)
Console.Out.WriteLine("empty");
else
{
Console.Out.WriteLine("not empty"); Console.Out.WriteLine(abc[i]);}
Console.In.Read();}
}