在java中打印使用long[]构建的数组
我尝试使用几种不同的方法来打印我的数组,但似乎一切都搞砸了。以前我能从中得到一些东西,现在我只能得到零。如果有人知道一种轻松打印数组的方法,那就太好了。由于某种原因,使用 Array.toString 似乎不起作用。
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();
}
}
}
我不断收到错误消息,说它必须是数组类型,但已解析为数组。我只是需要它来看看其他东西是否有效。
I have tried to print my array using a few different methods and seem to have screwed it all up. I used to get something back out of it and now I can only get zero's back out. If anybody knew a way to print the array, easily, that would be great. Using Array.toString doesn't seem to work for some reason.
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();
}
}
}
I kept getting an error saying it has to be an array type but resolved to Array. I just need it to see if anything else is even working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对实际发生的情况的回答低于规则。据推测,这只是用于学习 Java 的代码 - 否则 ArrayList 类会更合适。您应该尝试将其用作调试练习...什么可以帮助您找出问题所在(例如日志记录、使用调试器单步执行)?如何一次解决一个问题?您如何对代码进行单元测试?这样,您将了解到的不仅仅是特定代码的问题所在。
您尚未重写
Array
类中的toString
。如果添加此方法:那么它会打印值。由于
sorter
方法的原因,它并没有完全打印出您期望的值,但这是一个单独的问题。目前,您的
sorter()
方法 - 实际上可能会或可能不会正确排序,我还没有检查过 - 对数组的整个进行排序,而不仅仅是大小。这意味着您最终会将“有效”值排序到数组的“无效”部分中,然后稍后将其覆盖。
如果您只是将限制更改为使用
size
而不是InitialArray.length
,它似乎可以工作 - 但正如我所说,我还没有检查排序本身。Answer to what's actually going on is below the rule. Presumably this is only code for learning about Java - as otherwise the
ArrayList
class would be more suitable. You should try to use this as an exercise in debugging... what would help you to work out what was going wrong (e.g. logging, stepping through with a debugger)? How can you tackle one problem at a time? How could you unit test the code? That way you'll learn more than just what was wrong with your specific code.You haven't overridden
toString
in theArray
class. If you add this method:then it prints values. It doesn't quite print the values you were expecting, due to the
sorter
method, but that's a separate problem.Currently your
sorter()
method - which may or may not actually be sorting correctly, I haven't checked - sorts the whole of the array, not just up tosize
. That means you end up sorting your "valid" values into the "invalid" part of the array which you then overwrite later.If you just change your limits to use
size
instead ofInitialArray.length
it seems to work - but as I say, I haven't checked the sorting itself.在 Array 类中使用 java.util.Arrays.toString(long[]) 方法并重写 toString() 方法。
我已经在 sorter() 方法中进行了更正,以确保只应排列“有效”元素。
Use
java.util.Arrays.toString(long[])
method and also overridertoString()
method in your Array class.I've made correction in sorter() method to make sure that only "valid" elements are should be arranged.
我没有看到您的 Arrays 类实现 toString() 方法。您应该重写类中的 toString() 方法,并在实现中打印您希望看到的状态。
在这个特定的示例中,我假设它将打印长值数组。
Arrays.toString(long[] array) 应该可以帮助你。
I don't see your Arrays class implement a toString() method. You should override the toString() method in your class and in the implementation print the state that you wish to see.
In this particular example, I assume it would be printing the array of long values.
Arrays.toString(long[] array) should help you with that.