Hi there.

i am trying to create an java class dealing with arrays though i am only new to programming.

i have created a class with a char array field though i am not sure how to initialise it in the constructor.
i am also trying to implement a few methods though a couple i am lost on:
•print(). This method prints the string it represents to the console.
•concat().This method takes a char array as a parameter. It returns a new char array, which is the concatenation of MyString and the input char array. E.g. if MyString represents “Memorise “, and the input parameter represents the “syntax”. This method will return “Memorise syntax”.
•subString().This method takes a char array as parameter. It returns true if the input char array is a sub-string of MyString. Otherwise it returns false.

any help in any way would be greatly appreciated.
Cheers Nate
below is what i have got so far, (though not sure if right)

public class MyString
{
private char[] Array1;


public int length;


public MyString(char[] Array1)// my constructor though think i am way off
{


this.Array1 = Array1;
}


public int length() //returns length of array
{
length = Array1.length;
return length;
}


public char[] copy() // copy's the entire array
{
char[] copy =new char[Array1.length];
for (int i=0; i > Array1.length; i++)
{
copy = Array1;
}
return copy;
}


public char duplicate(char j, int i) // duplicates and object in the array
{
Array1 = j;
return j;


}

//now iam lost

Edited 3 Years Ago by happygeek: fixed formatting

well, first and foremost, use [ code][ /code] tags. now that that's out of the way,

your constructor is right

for print you need to use a for loop or a while loop, to loop through the array and print the individual chars one at a time

for concat you need to make a new local array that is of size length + parameter length and copy the first into it and then the second.

for substring... that one is a bit more difficult, you need a local boolean to keep track of whether it is part or not, you need a a couple of loops, and a few int variables, i cannot think of a way to explain the way i am thinking it other than making code, but i think that you should try first :)

EDIT: on an off-topic note, what IDE are you using? NetBeans will write a constructor for you if you know hoe to use the right features

For simplicity, I helped a bit with subString, though I haven't tested it thoroughly enough to track if it will always be true. I did however provide an explanation on what was done to come up with a method solution--

/**
Hi there.

i am trying to create an java class dealing with arrays though i am only new to programming.

i have created a class with a char array field though i am not sure how to initialise it in the constructor.
i am also trying to implement a few methods though a couple i am lost on:
•print(). This method prints the string it represents to the console.
•concat().This method takes a char array as a parameter. It returns a new char array, which is the concatenation of MyString and the input char array. E.g. if MyString represents “Memorise “, and the input parameter represents the “syntax”. This method will return “Memorise syntax”.
•subString().This method takes a char array as parameter. It returns true if the input char array is a sub-string of MyString. Otherwise it returns false.

any help in any way would be greatly appreciated.
Cheers Nate
below is what i have got so far, (though not sure if right)
*/

public class MyString
{
	private char[] Array1;

	//public int length; // unnecessary

	public MyString(char[] Array1)// my constructor though think i am way off
	{
		this.Array1 = Array1;
	}

	public MyString(){
		this(new char[0]); // initializing a char array with no elements, but reference-variable is not nul
	}

	public static void main(String... args){
		System.out.println(new MyString("Hello".toCharArray()).subString("ell".toCharArray()));
	}

	public int length() //returns length of array
	{
		//length = Array1.length; // unnecessary
		return Array1.length;
	}

	public char[] copy() // copy's the entire array
	{
		char[] copy =new char[Array1.length];
		for (int i=0; i < Array1.length; i++) // changed greater than to less-than.
		{
			copy[i] = Array1[i];
		}
		return copy;
	}

	/*
	 * Wait, what? What is duplicate supposed to do exactly?
	 */
	public char duplicate(char j, int i) // duplicates and object in the array
	{
		Array1[i] = j;
		return j;

	}
	//now iam lost

	/*
	 * Here's a hint for concat
	 */
	public char[] concat(char[] other){
		// instantiate a new char array that is length (Array1.length + other.length)
		// copy the elements of Array1 into the newly generated array (only up to Array1.length - 1), use a for loop.
		// afterwards copy the elements of the other array to the remaining parts of the newly generated array, use a for loop
		return new char[0]; // change this line to return the newly generated array with the values of MyString and other combined
	}

	public void print(){
		// iterate through all of the elements of the char array and use System.out.print(Array1[i]) to print the value
		// don't use System.out.println() until after all of the elements in Array1 are printed out
	}

	public boolean subString(char[] other){
		// because this can be fairly complicated, I will attempt to write a simple approach for the problem

		if(other.length > Array1.length) // if the argument array is bigger then there's no chance in hell that it will be a sub string!
			return false;

		for(int i = 0; i < Array1.length; i++){
			int match = 0; // a data type to store the amount of matches that occurred
			for(int j = 0; j < other.length && j < (Array1.length - i); j++){
				if(Array1[j + i] == other[j]){
					match++;
				}
			}

			if(match == other.length) // if the matched characters are equal to the length of the other array, then obviously the sub exists!
				return true;
		}

		return false; // if no matches match the length of the other char array, return false.
	}

}

i was thinking something a bit different, but the code looks close to yours, i think i just used a different strategy the below code is the same as yours with the exception of the substring method, which i rewrote my way. and the removal of your comments, as they already are in the thread

public class MyString {
	private char [ ] Array1 ;

	public MyString ( char [ ] Array1 )// my constructor though think i am way off
	{
		this.Array1 = Array1 ;
	}

	public MyString ( ) {
		this ( new char [ 0 ] ) ; // initializing a char array with no elements, but reference-variable is not nul
	}

	public static void main ( String ... args ) {
		System.out.println ( new MyString ( "Hello".toCharArray ( ) )
				.subString ( "lell".toCharArray ( ) ) ) ;
	}

	public int length ( ) //returns length of array
	{
		return Array1.length ;
	}

	public char [ ] copy ( ) // copy's the entire array
	{
		char [ ] copy = new char [ Array1.length ] ;
		for ( int i = 0 ; i < Array1.length ; i ++ ) // changed greater than to less-than.
		{
			copy [ i ] = Array1 [ i ] ;
		}
		return copy ;
	}

	public char duplicate ( char j , int i ) // duplicates and object in the array
	{
		Array1 [ i ] = j ;
		return j ;
	}

	public char [ ] concat ( char [ ] other ) {
		return new char [ 0 ] ; // change this line to return the newly generated array with the values of MyString and other combined
	}

	public void print ( ) {
	// iterate through all of the elements of the char array and use System.out.print(Array1[i]) to print the value
	// don't use System.out.println() until after all of the elements in Array1 are printed out
	}

	public boolean subString ( char [ ] other ) {
		//My thoughts
		boolean found = false ;
		//start looping through this array
		for ( int i = 0 ; i < Array1.length ; i ++ ) {
			if ( Array1 [ i ] == other [ 0 ] ) {
				//if the current position is the same as the first of the
				//input string, start another loop
				int j = 1 ;
				i ++ ;
				//loop forever
				while ( true ) {
					if ( Array1 [ i ] != other [ j ] ) {
					    //if current is not the same as previous
					    //stop loop
						break ;
					} else {
					    
						i ++ ;
						j ++ ;
						if ( i < Array1.length ) {
							//if we are not out of bounds see if
							//we are at the end of the input string
							if ( j == other.length ) {
								//if we are return true, because there 
								return true ;
							} else {
								continue ;
							}
						} else {
							return false ;
						}
					}
				}
			}
		}
		return false ;
	}
}

EDIT: WOW! that looked a whole lot better without word wrap

Hey thx heaps guys.

though i am still unsure about the subString.
i am only new to coding and are unsure on how the operation actually works.
could someone please expand on the notes given lol

cheers
nate

which one do you want expanded, i assume Alex's, as his is shorter...

where/how are you learning?

yeah alex's substring

Just using a few online resources to get a head start for a course i am doing soon

cheers
nate

Hey thx heaps guys.

though i am still unsure about the subString.
i am only new to coding and are unsure on how the operation actually works.
could someone please expand on the notes given lol

cheers
nate

The logic is pretty simple.

A subString suggests that the argument String is either the String or a "chunk" of the String the argument is matching against.

The first test is therefore to see if the argument is less than the String to be matched against. If that fails there's no point in going on.

Now you need to test the consecutive sequence of characters between a range of character within the String to be matched against. If that consecutive sequence of characters matches the consecutive sequence of characters of the argument char array, then the subString exists (and the method returns true).

If no matches were found for all ranges of a character index i to the end of the String, then there is no subString occurrence of the argument String.

There's also a safety net involved in my algorithm - basically if the range of Array1-i is less than the argument String that is matching against it then there's the risk of a null pointer exception when iterating through the indices of the argument array in comparison to a char-index of the String it is matching against to the length of the argument char-array.

That's about it.

This article has been dead for over six months. Start a new discussion instead.