Page 1 of 1

Arrays. What am I doing wrong?

Posted: Wed Jul 22, 2020 8:58 am
by Adash
Could anyone please confirm how I am supposed to use arrays?
I'm just trying to create arrays, adding items (defining a fixed array with all values would be nice instead of adding values seperately)
Then I'd like to use collections.shuffle
then I'd like to just use basic functions like myarray.contains(x) to return a bolean and myarray.get(x) to return an item at a specified index. All pretty simple stuff.

Problem I'm having is first I created my arrays OK but had to include java.util (is this OK?)
But then when comparing any values the whole thing broke and I was getting the array raw type errors.
So I though it might be because I was comparing a mix of int and double so I tried creating an array of double type but found out online arrays dont take double (raw type) but in later versions I could leave it unspecified (<>) and it would work it out itself. But then I got errors with that too.
Heres the sort of thing I'm up to at the moment (without the get() or collections.shuffle() etc:
ArrayList<> CH14list = new ArrayList<>();
for (int i = 1; i <= CH1lengthknob.GetValue(); i++) {
CH14list.add(i);
}


Could anyone please advise or point me to the best docs that show me how I am supposed to be working with arrays so I can do it 'properly'. I am getting so much conflicting advise on the internet and none of it is working.

I am using OpenJDK 14.0.1

Thanks

Re: Arrays. What am I doing wrong?

Posted: Wed Jul 22, 2020 12:30 pm
by arbuxMusic
What's your background in programming? That might help with recommendations to learn.

For some simple reference material, I suggest maybe look at W3Schools (e.g. https://www.w3schools.com/java/java_arraylist.asp) or YouTube. If you can get it on sale, the Udemy Java Masterclass is really in-depth.

Some example code (I put this in the module's Initialize function to test). As you note, this imported java.util.* (for ease) to allow the ArrayList type to be referred to in the code.

Code: Select all

      // Initialize a new ArrayList
      var CH14list = new ArrayList<Integer>();
      
      // Populate the array with incremental numbers up to the value of the knob
      for (int i = 1; i <= (int)(CH1lengthknob.GetValue()); i++) {
         CH14list.add(i);
      }

      // Write out the unshuffled list to the console
      Log("Unshuffled:" + CH14list);
      
      // Shuffle the list
      Collections.shuffle(CH14list);
      
      // Write out the shuffled list to the console
      Log("Shuffled:" + CH14list);
Note that arrays / ArrayLists for Java are zero-index. The first element is at position 0.

If you can post the whole code here, I can help with debugging (likely over the next couple of days :) )

Re: Arrays. What am I doing wrong?

Posted: Wed Jul 22, 2020 12:36 pm
by josephbottinger
Note that in Java, an *array* and a *list* are different things. An array is a fixed-size datatype; a List is not.

You can use List.of(1,2,3,4,5) to get a List<Integer>, but it's unmodifiable.

The advice to do the basic tutorials is probably well-founded; Java's verbose but that makes doing a lot of things very easy, because you don't have to assume you know what it's doing (it will do what you tell it to.)

I'd suggest the Oracle tutorial for Java over w3schools, personally - https://docs.oracle.com/javase/tutorial/ - but I don't think it makes a ton of difference which one you use. They're both pretty fast reads, largely because Java's so simple.

Re: Arrays. What am I doing wrong?

Posted: Wed Jul 22, 2020 9:04 pm
by Adash
Thanks for the suggestions. I already use Udemy so I'll give that one a try.

Re: Arrays. What am I doing wrong?

Posted: Wed Jul 22, 2020 9:24 pm
by Adash
The Oracle tutorials are great too. Bookmarked. Thanks

Re: Arrays. What am I doing wrong?

Posted: Sun Jul 26, 2020 9:28 am
by Adash
Just to update, I now undetsrand how to implement arrays, and the difference between arrays and Collections :-)
Thanks again :-)