Java class returns pointer instead of string -
this question has answer here:
i want stress know how solve this problem if modify code instructor has given me, not option. should stress don't have experience java.
so have been given assignment complete project skeleton code provided me. output getting running it:
genomic sequence: [c@4aa298b7 sequence length: 25 array of exon positions: [1, 5, 8, 10, 13, 16]
as can see, "genomic sequence" field printing pointer instead of valid data should more "aaaggttatta..."
this information printed out:
public class bioseqdata { public static void main(string[] args) { string demodna = new string("aatgccagtcagcatagcgtagact"); int[] ardemo = {1, 5, 8, 10, 13, 16}; genomicdnasequence gdemo = new genomicdnasequence(demodna.tochararray()); system.out.println( "genomic sequence: " + gdemo ); ...
if modify class in assignment print out pointer pointing instead of pointer itself, would, not option. how can change genomicdnasequence return char array instead of pointer?
here relevant parts of genomic dna sequence:
public class genomicdnasequence extends dnasequence { public boolean[] iscoding; // made public instead of private grading. public genomicdnasequence(char[] gdnaarr) { super(gdnaarr); } ...
and class extends:
public class dnasequence extends sequence { public dnasequence(char[] dnaarr) { super(dnaarr); } ...
and class that extends:
public class sequence { public char[] seqarr; // made public instead of protected grading. public sequence(char[] sarr) { for(int i=0;i<sarr.length;i++){ if (!isvalidletter(sarr[i])){ throw new illegalargumentexception("invalid sequence of letters class edu.iastate.cs228.hw1.sequence"); } } seqarr=sarr; ...
from can tell, have access genomicdnasequence
not of other classes. when try print genomicdnasequence
object, somehow seeing output looks array.
this suggests 1 of ancestor classes of genomicdnasequence
has tostring
method uses default conversion of array string. in java, if a
array, a.tostring()
looks weird. need override genomicdnasequence object's tostring
method returns cleaner instance of sequence string.
in particular go with
public class genomicdnasequence extends dnasequence { public boolean[] iscoding; // made public instead of private @override public string tostring() { return new string(seqarr); } ... }
this makes character array ['g', 'c', 'a', 't']
turn string "gcat"
.
addendum
it important use new string
, not arrays.tostring()
. example:
import java.util.arrays; public class myclass { public static void main(string args[]) { char[] c = {'g', 'c', 'a', 't'}; system.out.println(c.tostring()); system.out.println(arrays.tostring(c)); system.out.println(new string(c)); } }
produces
[c@2a139a55 [g, c, a, t] gcat
Comments
Post a Comment