Ok, here is the code and then the discussion follows:
public class FlatArrayList {
private static ArrayList<TestWrapperObject> probModel = new ArrayList<TestWrapperObject>();
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] currentRow = new int[10];
int counter = 0;
while (true) {
for (int i = 0; i < 10; i++) {
currentRow[i] = probModel.size();
}
TestWrapperObject currentWO = new TestWrapperObject(currentRow);
probModel.add(counter, currentWO);
TestWrapperObject testWO = probModel.get(counter);
// System.out.println(testWO);
counter++;
if (probModel.size() == 10) break;
}
// Output the whole ArrayList
for (TestWrapperObject wo:probModel) {
int [] currentTestRow = wo.getCurrentRow();
}
}
}
public class TestWrapperObject {
private int [] currentRow;
public void setCurrentRow(int [] currentRow) {
this.currentRow = currentRow;
}
public int [] getCurrentRow() {
return this.currentRow;
}
public TestWrapperObject(int [] currentRow) {
this.currentRow = currentRow;
}
}
What is the above code supposed to do? What I am trying to do is load an array as a member of some wrapper object (TestWrapperObject in our case). When I get out of the loop,
the probModel ArrayList has the number of elements it is supposed to have but all have the same value of the last element (an array of size 10 with each item equal to 9). This is not the case inside the loop. If you perform the same "experiment" with a primitive int value everything works fine. Am I missing something myself regarding arrays as object members? Or did I just encounter a Java bug? I am using Java 6.
You are only creating one instance of the currentRow array. Move that inside the row loop and it should behave more like you expect.
Specifically, the assignment in setCurrentRow does not create a copy of the object, but only assigns the reference. So each copy of your wrapper object will hold a reference to the same int[] array. Changing the values in that array will make the values appear to change for all other wrapper objects that hold a reference to the same instance of the array.
i don' t want to sound condescending, but always try to remember tip #26 from the excellent pragmatic programmer book
select isn't broken
it is very rare to find a java bug. keeping this in mind often helps me to look over my code again, turn it around, and shake out the loose bits until i finally discover where i was wrong. of course asking for help early enough is very encouraged, too :)
Related
What I want to do: read strings from keyboard using Scanner until specific string is used to break the infinite loop. save them in an arrayList, then pass them into an array with its length beeing the number of the iteration of the loop
public class InputReader {
ArrayList<Integer> list = new ArrayList<>(0);
int arrayLength;
String readInput;
Scanner ir = new Scanner(System.in);
void readInput() {
for (int m=0; ;m++) {
readInput = ir.nextLine();
if ("q".equals(readInput)) {
//problem: arrayLength does not have the value of m outside the loop
arrayLength = m;
break;
}
System.out.println("arrayLength: "+arrayLength);
intInput = Integer.parseInt(readInput);
list.add(intInput);
}
}
int[] array = new int[arrayLength];
}
}
Inside the loop arrayLength works perfectly but outside the loop it has no value as I initialized it without value. Because of this,
System.out.println("array.length: "+array.length);
always returns 0 and the compiler returns this error:
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
when I try to save integers inside the array.
1) How can I make the changes to the variable stay outside the loop?
2) Weird observation A:
int[] array = new int[list.size()];
returns the same error despite list.size() having the right value even outside the loop (checked by printing it).
And B: The code works if I create my array inside another method instead of inside the class and then use it in the method, but this way I cannot use it in my other classes despite using inheritance or parameters.
void giveOutput() {
int[] array = new int[list.size()];
public void giveOutput () {
System.out.println("list.size()"+list.size());
System.out.println("array.length:"+array.length);
for (int n=0; n<list.size(); n++) {
array[n] = list.get(n);
System.out.print("array["+n+"]:"+array[n]+" ");
}
}
}
this creates a working array but I cant hand it over to my Minsort extends InputReader subclass where it is sorted which leads to question number
3) How to use variables initialized in methods in other classes? This way my program could work too.
(I am a bloody beginner, started seriously working with java yesterday, my first succesful project was a Minsort-Algorithm I wrote from scratch so please have mercy. And thanks in advance.)
The array array is initialized when an object of the type InputReader is instantiated, not after the readInput() method is run.
In other words, array is always initialized when you use new InputReader(), not after the readInput() method is run. That means that the arrayLength variable has not yet been modified at the moment when array is created, so the default value for an int is used, which is 0.
Try this:
public static class InputReader {
ArrayList<Integer> list = new ArrayList<>(0);
int arrayLength;
String readInput;
Scanner ir = new Scanner(System.in);
int[] array;
void readInput() {
for (int m=0; ;m++) {
readInput = ir.nextLine();
if ("q".equals(readInput)) {
//problem: arrayLength does not have the value of m outside the loop
arrayLength = m;
break;
}
int intInput = Integer.parseInt(readInput);
list.add(intInput);
}
System.out.println("arrayLength: "+arrayLength);
array = new int[arrayLength];
}
}
Also, your System.out.println("arrayLength: "+arrayLength); always returned 0 because the arrayLength changes only when q is pressed.
To answer your third point: You could create a getter function in the class InputReader. For example:
public int[] getArray() {
return array;
}
As a side-note: It is very good practice to have your instance variables be declared with a private access modifier, and to then create public getter and setters. This is called Encapsulation, and is an OOP principle (my advice is that you google the OOP principles).
More info about getters and accessors here: Why use getters and setters/accessors?
I do not understand the details of using an array as a parameter:
I have created an int array and a method and I do not understand why it's possible to rename the parameter as seen below from "note" to "veraenderung".
How does Java deal with parameters? Do I need a parameter even if I call the method(note)?
public class ArrayParameter {
public static void main(String[] args) {
int[] note = {3,2,1,4,5}; //Array
int[] note2 = {3,2,1,4,5};
korrektur(note);
korrektur2(note2);
}
//Der Methode "korrektur" wird der int Array (note) übergeben!
public static void korrektur (int note[]) {
for (int i = 0; i<note.length; i++) {
//Sobald die for Schleife aufgerufen wird, werden die jeweiligen Werte um -1 reduziert
note[i]-=1;
System.out.println(note[i]);
}
}
//int "veraenderung" ist ein Parameter
public static void korrektur2 (int veraenderung[]) {
for (int i = 0; i<veraenderung.length; i++) {
//Sobald die for Schleife aufgerufen wird, werden die jeweiligen Werte um -1 reduziert
veraenderung[i]-=1;
System.out.println(veraenderung[i]);
}
}
}
i do not understand why it is possible to rename the parameter
korrektur and korrektur2 are just 2 different methods each of them having 2 independent signatures. They are not related whatsoever (even if the programmer can name them alike, just as you did);
The name of the parameters are just local names useful in the method block;
How do java deal with parameter? Do i need a parameter even if i call the method(note)?
Those methods parameters are mandatory ones. Hence you need to insert them in your method call.
I will try to break it down as small as possible for you to understand :)...
When you creating the method. example: public static void korrektur2 (int veraenderung[]) the int veraenderung[] can be named whatever you want...the main thing is that you put the "int []" somewhere within the brackets so that you tell java that "hey my method take an integer array".
The name veraenderung itself is only used locally (within that method) as a reference to tell java whatever you parse into that method will act.
For example when you call the method above and said korrektur(note); . note was declared as an array before, so when you put 'note' inside the method korrektur it is the same as replacing veraenderung with note. In other words java replaces your local variable with the one you passed into the method.
And to answer your question....for now...since you created the method which takes an integer array...YES you must always "put an integer array inside" it when calling the method. I.e. every time you call the method korrektur2 or korrekturand you do not put an integer array inside it, Java would be like "hey I was created to take an integer array and do stuff with it...why are you leaving me empty ?!?!?"
I hope this solved your questions :)
If you have seen the .class file, you'll find the parameters are the same.
you should understand the conception 'vars Scope' (may be this). Parameter 'paramArrayOfInt' have the influence korrektur(), then it be destroied. In next method ,it just new one. It's my understanding. May be wrong, welcome to discuss.
public class ArrayParameter
{
public static void main(String[] paramArrayOfString)
{
int[] arrayOfInt1 = { 3, 2, 1, 4, 5 };
int[] arrayOfInt2 = { 3, 2, 1, 4, 5 };
korrektur(arrayOfInt1);
korrektur2(arrayOfInt2);
}
public static void korrektur(int[] paramArrayOfInt)
{
for (int i = 0; i < paramArrayOfInt.length; i++)
{
paramArrayOfInt[i] -= 1;
System.out.println(paramArrayOfInt[i]);
}
}
public static void korrektur2(int[] paramArrayOfInt)
{
for (int i = 0; i < paramArrayOfInt.length; i++)
{
paramArrayOfInt[i] -= 1;
System.out.println(paramArrayOfInt[i]);
}
}
}
I recently came across a very stupid (at least from my point of view) implementation inside Androids Parcel class.
Suppose I have a simple class like this
class Foo implements Parcelable{
private String[] bars;
//other members
public in describeContents(){
return 0;
}
public void writeToParcel(Parcel dest, int flags){
dest.writeStringArray(bars);
//parcel others
}
private Foo(Parcel source){
source.readStringArray(bars);
//unparcel other members
}
public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>(){
public Foo createFromParcel(Parcel source){
return new Foo(source);
}
public Foo[] newArray(int size){
return new Foo[size];
}
};
}
Now, if I want to Parcel a Foo Object and bars is null I see no way to recover from this situation (exept of catching Exceptions of course). Here is the implementation of these two methods from Parcel:
public final void writeStringArray(String[] val) {
if (val != null) {
int N = val.length;
writeInt(N);
for (int i=0; i<N; i++) {
writeString(val[i]);
}
} else {
writeInt(-1);
}
}
public final void readStringArray(String[] val) {
int N = readInt();
if (N == val.length) {
for (int i=0; i<N; i++) {
val[i] = readString();
}
} else {
throw new RuntimeException("bad array lengths");
}
}
So writeStringArray is fine if I pass bars which are null. It just writes -1 to the Parcel. But How is the method readStringArray supposed to get used? If I pass bars inside (which of course is null) I will get a NullPointerException from val.length. If I create bars before like say bars = new String[???] I don't get any clue how big it should be. If the size doesn't match what was written inside I recieve a RuntimeException.
Why is readStringArray not aware of a result of -1 which gets written on null objects from writeStringArray and just returns?
The only way I see is to save the size of bars before I call writeStringArray(String[]) which makes this method kind of useless. It will also redundatly save the size of the Array twice (one time for me to remember, the second time from writeStringArray).
Does anyone know how these two methods are supposed to be used, as there is NO java-doc for them on top?
You should use Parcel.createStringArray() in your case.
I can't imagine a proper use-case for Parcel.readStringArray(String[] val) but in order to use it you have to know the exact size of array and manually allocate it.
It's not really clear from the (lack of) documentation but readStringArray() is to be used when the object already knows how to create the string array before calling this function; for example when it's statistically instanciated or it's size is known from another previously read value.
What you need here is to call the function createStringArray() instead.
this may seem daft i have a class called ship locations which i wish to store all my ships locations, ive extended it from my client class and simply called the set method as follows
sub.local being a multidimensional array from the ship class
sub.local = new int[2][2];
sub.local[0][0] =row;
sub.local[0][1]=col;
sub.local[1][0]=row;
sub.local[1][1] =col+1;
toServer.writeInt(row);
toServer.writeInt(col);
toServer.writeChar('s');
sub.placed=true;
setp1sub(sub.local);
When i print it back through another class it comes back with the location in the memory rather than the numbers i need. What is the reason for this
public class ShipLocations {
static int [][] p1sub;
public ShipLocations()
{
p1sub = new int[2][2];
}
public int[][] getp1sub()
{
return p1sub;
}
public void setp1sub(int[][] local) {
for (int i = 0;i <local.length;i++)
{
for(int j = 0;j<local.length;j++)
{
p1sub [i][j]= local[i][j];
}
}
}
}
would it be that im passing it as sub.local ?
output is [[I#a401c2
Instead of writing
System.out.println(yourArray);
use
// for multidemensional arrays:
System.out.println(Arrays.deepToString(yourArray));
// or for one dimemsional arrays:
System.out.println(Arrays.toString(yourArray));
Here is a link to the relevant JavaDoc.
For an explanation of your output, you can look at this answer.
So I'm creating a class called dicegame. Here's the constructor.
public class dicegame {
private static int a, b, winner;
public dicegame()
{
a = 0;
b = 0;
winner = 2;
}
And now in the main, I'm creating an array of this object (I called it spaghetti for fun).
public static void main(String[] args)
{
dicegame[] spaghetti = new dicegame[10];
spaghetti[1].roll();
}
But when I try to do anything to an element in the array, I'm getting the NullPointerException. When I tried to print one of the elements, I got a null.
You created an array, but you have to assign something (e.g. new dicegame()) to each element of the array.
My Java is slightly rusty, but this should be close:
for (int i=0; i<10; i++)
{
spaghetti[i] = new dicegame();
}
new dicegame[10]
just creates an array with 10 empty elements. You still have to put a dicegame in each element:
spaghetti[0] = new dicegame();
spaghetti[1] = new dicegame();
spaghetti[2] = new dicegame();
...
You need spaghetti[1]=new dicegame() before you call roll() on it.
Right now you are allocating an array,but don't. Place any objects in this array, so by default java makes them null.
1.you have just declared the array variable but not created the object yet. try this
2.you should start index with zero not with one.
dicegame[] spaghetti = new dicegame[10]; // created array variable of dicegame
for (int i = 0; i < spaghetti.length; i++) {
spaghetti[i] = new dicegame(); // creating object an assgning to element of spaghetti
spaghetti[i].roll(); // calling roll method.
}
Firstly,you should create object for every spaghetti input of yours.
You can start with whatever value you want. Just be sure that the size of array is matched accordingly so that you won't get ArrayIndexOutOfBounds Exception.
So,if you wanted to start with 1 and have 10 objects of the class dicegame,you will have to assign the size of the array as 11(since it starts from zero).
your main function should be like :
public static void main(String[] args)
{
dicegame[] spaghetti = new dicegame[11];
//the below two lines create object for every spaghetti item
for(int i=1;i<=11;i++)
spaghetti[i]=new dicegame();
//and now if you want to call the function roll for the first element,just call it
spaghetti[1].roll;
}