2011-09-27 64 views
0

我试图用几种不同的方法打印我的数组,并且似乎已经把它搞砸了。我曾经从中得到一些东西,现在我只能得到零。如果有人知道打印阵列的方法,很容易,那会很棒。使用Array.toString似乎没有工作出于某种原因。在java中使用long []打印数组

public class Array { 
private long[] InitialArray; 
private int size = 0; 

/** 
* the default constructor 
*/ 
public Array(int size){ 
    InitialArray = new long[size]; 
} 

/** 
* this will scan in integers from a file 
* @param scans 
*/ 
public Array(Scanner scans){ 
    this(1); 
    int j = 0; //just used as a counter basically 
    while(scans.hasNext()){ //grabs each element 
     this.insert(Integer.parseInt(scans.nextLine())); //inserts it 
     j++; //steps through using the counter 
    } 
} 

/** 
* this will make the array longer if more space is needed so you don't 
* crash if you have a very large set of numbers 
*/ 
public void stretch(){ 
    long[] temp = new long[InitialArray.length *2]; //makes a temp array double the normal size 
    for(int i=0; i < InitialArray.length; i++) 
     temp [i] = InitialArray[i]; //steps through the old array to keep the data 
    InitialArray = temp; //assigns the temp array to the new array 
} 

/** 
* this will insert each element read, from a file, into the array 
* @param x 
*/ 
public void insert(int x){ 
    if (this.size+1 == InitialArray.length){ //trying not to crash 
     stretch(); //making sure I don't crash 
    } 
    InitialArray[size++] = x; //stepping through the numbers and inserting 
    sorter(); 
} 

/** 
* this is a selection sort on the array 
* It's not the quickest, but it's fairly easy to build and understand 
* Just finds the minimum element and swaps it to it's proper place 
* @return 
*/ 
public long[] sorter(){ 
    for (int i=0; i<InitialArray.length-1; i++){ 
     int minimum = i; //assigning a minimum 
     for (int j=i+1; j<InitialArray.length; j++){ 
      if (InitialArray[minimum] > InitialArray[j]) { //checking to make sure it is the smallest 
       minimum = j; //if it isn't then do this 
      } 
     } 
     if (minimum != i){ //all the swapping stuff, which I never really understand but it works 
      long temp = InitialArray[i]; 
      InitialArray[i] = InitialArray[minimum]; 
      InitialArray[minimum]= temp; 
     } 
    } 
    return InitialArray; 
} 
/** 
* @param args 
    */ 
public static void main(String[] args) { 
Scanner scans; 
try { 
    scans = new Scanner(new FileInputStream("src/some numbers.txt")); 
    Array InitialArray = new Array(scans); 
    System.out.println(InitialArray); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 
} 

我不断收到一个错误,说它必须是数组类型,但解析为数组。我只是需要它来查看是否还有其他工作。

回答

3

对实际情况的回答是低于规则。大概这只是学习Java的代码 - 否则ArrayList类会更合适。你应该尝试将它用作调试练习......什么可以帮助你找出问题所在(例如,记录,通过调试器进行)?你怎么一次解决一个问题?你怎么能单元测试代码?这样,您将学到的不仅仅是特定代码的错误。


您没有在Array类中重写toString。如果您添加此方法:

@Override public String toString() { 
    StringBuilder builder = new StringBuilder(); 
    for (int i = 0; i < size; i++) { 
     if (i > 0) { 
      builder.append(" "); 
     } 
     builder.append(InitialArray[i]); 
    } 
    return builder.toString(); 
} 

然后它会打印值。由于sorter方法的原因,它没有打印出您期望的值,但这是一个单独的问题。

目前您sorter()方法 - 这可能会或可能不会真的被排序正确,我没有检查 - 排序数组的整个,不仅达size。这意味着你最终将你的“有效”值排序到数组中的“无效”部分,然后你会覆盖它。

如果你只是改变你的限制使用size而不是InitialArray.length它似乎工作 - 但正如我所说,我没有检查排序本身。

+0

谢谢,你是对的我的排序不正确。这仅仅是为了学习一些东西。我可以尝试将其更改为一个ArrayList,然后我不必担心它的大小,并可以摆脱我的整个拉伸和一切。我会尽力做到这一点。 – Defc0n

1

我没有看到你的Arrays类实现了一个toString()方法。您应该重写类中的toString()方法,并在实现中打印您希望看到的状态。

在这个特殊的例子中,我假设它将打印长数组的数组。 Arrays.toString(long []数组)应该可以帮助你。

2

在您的Array类中使用java.util.Arrays.toString(long[])方法并覆盖toString()方法。

我已在sorter()方法中进行了更正,以确保只应安排“有效”元素。

public class Array 
{ 
public long[] sorter() 
{ 
    for (int i=0; i<size; i++) 
    { 
    int minimum = i; 
    for (int j=i+1; j<size; j++) 
    { 
     if (InitialArray[minimum] > InitialArray[j]) 
     { 
      minimum = j; //if it isn't then do this 
     } 
     } 
    if (minimum != i) 
    { 
     long temp = InitialArray[i]; 
     InitialArray[i] = InitialArray[minimum]; 
     InitialArray[minimum]= temp; 
     } 
    } 
    return InitialArray; 
} 

@Override 
public String toString() 
    { 
    return java.util.Arrays 
     .toString(java.util.Arrays.copyOf(InitialArray,size)); 
    } 
} 
+0

这也是我的第一本能,但它忽略了确定有多少值“有效”的大小变量。 –

+0

的确如此。 sorter()混合了数组的有效和无效“部分”。 – adatapost

+0

但即使不是这样,你最终会得到一堆0不是*逻辑*内容的一部分。 –