数字未添加到现有数组的末尾

huangapple go评论54阅读模式
英文:

Number is not added at back of existing array

问题

关于数组的学习。我无法弄清楚为什么不能将新的数字添加到现有数组的末尾。我在file_1.txt中读取了两个文本文件,其中包含数字'1 2 3',而在file_2.txt中包含数字'91'。基本上,如果没有“Void addBack()”方法,程序会按照我期望的方式运行,但是通过添加该方法,似乎不会创建新的数组。即使我遍历“elements[i] = elements[i-1]”,它也不会将其整体打印出来。我期望的是首先打印出第一部分“数字是:1 2 3”,然后打印出第二部分“数字是:1 2 3 91”。

public class ExampleLecture {
	
	IntRow readIntRow(Scanner input) {
		IntRow result = new IntRow();
		while (input.hasNext()) {
			result.add(input.nextInt());
		}
		return result;
	}
	
	IntRow setBack(Scanner input) {
		IntRow result = new IntRow();
		while(input.hasNext()) {
			result.addBack(input.nextInt());
			System.out.println("here");
		}
		return result;
	}
	
	void print(IntRow row) {
		for (int i = 0; i < row.numberOfElements; i++) {
			System.out.printf("%d ", row.elements[i]);
		}
		System.out.printf("\n");
	}
		
	void start() {
		
		Scanner in = UIAuxiliaryMethods.askUserForInput().getScanner();
		Scanner in2 =UIAuxiliaryMethods.askUserForInput().getScanner();
		IntRow row = readIntRow(in);
		IntRow row2 = setBack(in2);
		
		System.out.printf("数字是:");
		print (row);
		System.out.printf("新的数字是:");
		print (row2);
	}

	public static void main(String[] args) {
		new ExampleLecture().start();
	}
}
package examplelecture;

class IntRow {
	
	static final int MAX_NUMBER_OF_ELEMENTS = 250;
	
	int[] elements;
	int numberOfElements;
	
	IntRow() {
		elements = new int[MAX_NUMBER_OF_ELEMENTS];
		numberOfElements = 0;
	}
	
	void add(int number) {
		elements[numberOfElements] = number;
		numberOfElements += 1;
	}
	
	void addBack(int number) {
		for (int i = numberOfElements; i > 0; i--) {
		    elements[i] = elements[i-1];
		    elements[i] = number;
		}
	}
}
英文:

Learning about Arrays. I am not able to figure out why a new number is not added to the back of my existing array. I read in two textfiles in file_1.txt are the numbers '1 2 3' and in file_2.txt is the number '91'. Basically without the method of Void addBack() the program does what I expect, however by adding the method it seems not make a new Array. Even when I go over the elements[i] = elements[i-1] it won't print it as a whole. I am expecting to print for the first part
The numbers are: 1 2 3 and the second part The numbers are: 1 2 3 91.

public class ExampleLecture {
IntRow readIntRow(Scanner input) {
IntRow result = new IntRow();
while (input.hasNext()) {
result.add(input.nextInt());
}
return result;
}
IntRow setBack(Scanner input) {
IntRow result = new IntRow();
while(input.hasNext()) {
result.addBack(input.nextInt());
System.out.println(&quot;here&quot;);
}
return result;
}
void print(IntRow row) {
for (int i = 0; i &lt; row.numberOfElements; i++) {
System.out.printf(&quot;%d &quot;, row.elements[i]);
}
System.out.printf(&quot;\n&quot;);
}
void start() {
Scanner in = UIAuxiliaryMethods.askUserForInput().getScanner();
Scanner in2 =UIAuxiliaryMethods.askUserForInput().getScanner();
IntRow row = readIntRow(in);
IntRow row2 = setBack(in2);
System.out.printf(&quot;the numbers are: &quot;);
print (row);
System.out.printf(&quot;the new numbers are: &quot;);
print (row2);
}
public static void main(String[] args) {
new ExampleLecture().start();
}
}
    package examplelecture;
class IntRow {
static final int MAX_NUMBER_OF_ELEMENTS = 250;
int[] elements;
int numberOfElements;
IntRow() {
elements = new int[MAX_NUMBER_OF_ELEMENTS];
numberOfElements = 0;
}
void add(int number) {
elements[numberOfElements] = number;
numberOfElements += 1;
}
void addBack(int number) {
for (int i = numberOfElements; i&gt;0; i--) {
elements[i] = elements[i-1];
elements[i] = number;
}
}
}

答案1

得分: 1

以下是翻译好的部分:

你有两个连续的赋值语句写入相同的位置:

elements[i] = elements[i-1];
elements[i] = number;

这个值总是被number覆盖,所以第一个语句没有效果。

还有,在你的 addBack 方法中,你的循环是这样的:

for (int i = numberOfElements; i > 0; i--) {

如果 numberOfElements 是0会发生什么呢?

你称其为 addBack,但这个方法的更好名称似乎应该是 addFirst。通常,索引0被视为前面,而不是后面。

英文:

You have 2 successive assignments which write to the same position:

elements[i] = elements[i-1];
elements[i] = number;

The value is alway overwritten with number, so the first statement has no effect.

Also in your addBack method your for cycle:

for (int i = numberOfElements; i&gt;0; i--) {

What happens if numberOfElements is 0?


You call it addBack but it looks like a better name for the method is addFirst. Usually index 0 is considered the front, not the back.

答案2

得分: 0

"Admittedly it is not entirely clear what you are trying to accomplish. But you may have several problems. The first is as follows:

	IntRow setBack(Scanner input) {
		IntRow result = new IntRow();
		while (input.hasNext()) {
			result.addBack(input.nextInt());
			System.out.println("here");
		}
		return result;
	}

IntRow has nothing in it since it is new. So all you are doing is iterating over the new file which has just 91 in it. Remember, result has no items. So it won't even iterate once in addBack.

So just do the following:

Change your addBack method to just add the numbers. Why use a loop to cascade down the elements since you are doing this within the same instance of IntRow? Just add it on to the end using the numberOfElements as the next index.

void addBack(int number) {
    	elements[numberOfElements++] = number;        
}

If you want to copy the contents of one IntRow object to another you would need another method in the IntRow class. Something like:

public void copy(IntRow r) {
    for (int i = 0; i < r.numerOfElements; i++) {
        elements[i] = r.elements[i];
    }
    numerOfElements = r.numberOfElements;
}

And keeping with good design it might be better to return numberOfElements in a method such as public int size();"

英文:

Admittedly it is not entirely clear what you are trying to accomplish. But you may have several problems. The first is as follows:

	IntRow setBack(Scanner input) {
IntRow result = new IntRow();
while (input.hasNext()) {
result.addBack(input.nextInt());
System.out.println(&quot;here&quot;);
}
return result;
}

IntRow has nothing in it since it is new. So all you are doing is iterating over the new file which has just 91 in it. Remember, result has no items. So it won't even iterate once in addBack.

So just do the following:

Change your addBack method to just add the numbers. Why use a loop to cascade down the elements since you are doing this within the same instance of IntRow? Just add it on to the end using the numberofElements as the next index.

void addBack(int number) {
elements[numberOfElements++] = number;        
}

If you want to copy the contents of one IntRow object to another you would need another method in the IntRow class. Something like:

public void copy(IntRow r) {
for (int i = 0; i &lt; r.numerOfElements; i++) {
elements[i] = r.elements[i];
}
numerOfElements = r.numberOfElements;
}

And keeping with good design it might be better to return numberOfElements in a method such as public int size();

答案3

得分: 0

首先,readIntRow()setBack()两个方法都创建了新的IntRow对象rowrow2。如果您希望将结果附加到第一个创建的IntRow对象,即row,您应该调用:

        IntRow row = readIntRow(in);
        IntRow row2 = row.setBack(in2);

而且,需要修改setBack()方法如下:

    IntRow setBack(Scanner input) {
        while(input.hasNext()) {
            this.add(input.nextInt());
            System.out.println("here");
        }
        return this;
    }

请注意,在setBack()中,如果您想将数字追加到IntRow对象的末尾,您应该调用add(),而不是上面的addBack()。如果您想要在前面添加,您应该调用addBack()(最好将其命名为addFront())。

此外,在addBack()的实现中,如果您想在IntRow对象的前面添加元素,element[i] = number操作应该在循环之后仅发生一次。否则,索引小于或等于numberOfElements的所有值都将被数字覆盖。

    void addBack(int number) {
        for (int i = numberOfElements; i > 0; i--) {
            elements[i] = elements[i-1];
        }
        elements[0] = number;
    }
英文:

First off, both the readIntRow() and setBack() methods create new IntRow objects row and row2. If you want the result to be appended to the first IntRow object created i.e. to row , you should call:

        IntRow row = readIntRow(in);
IntRow row2 = row.setBack(in2);

and setBack() needs to be modified to:

    IntRow setBack(Scanner input) {
while(input.hasNext()) {
this.add(input.nextInt());
System.out.println(&quot;here&quot;);
}
return this;
}

Note that in setBack(), if you are trying to append numbers to the end of the IntRow object, you should call add() instead of addBack() as above. If you are trying to add to the front, you should call addBack() [and it might be better to call it addFront() instead].

Also, in the implementation of addBack(), if you are trying to add to the front of the IntRow object, the element[i] = number operation should take place only once, after the loop. Otherwise all the values in indices <= numberOfElements would be overwritten with number.

    void addBack(int number) {
for (int i = numberOfElements; i&gt;0; i--) {
elements[i] = elements[i-1];
}
elements[0] = number;
}

huangapple
  • 本文由 发表于 2020年8月10日 06:03:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定