Learning how to program in Java, using ArrayLists

I’m currently programming with Java, I’m slowly getting better, however, I’ve started learning newer things, can any of you see where I’m going wrong with the code below?

In my main method (currently testing):

ArrayList<Double> c1 = new ArrayList<>();
Iterator<Double> c1itr = c1.iterator();

// initialized i somewhere else
while(i < 200){
    c1.add(in.nextDouble());
    i++;
}

while(c1itr.hasNext()){
    double c1Temp = c1itr.next();
    c1Temp = c1Temp *0.1; 
}

Again, keep in mind this isn’t anywhere near finished, I know I can polish it up a lot, I just want to know how to get it to work, I’ve never used an iterator in Java before. The rest of it works fine, but I’m trying to iterate through the ArrayList, but using something like:

c1get(i = c1Temp);

This is the kinda thing that I want to get to work, but it doesn’t seem to be working for me, anyone got any way of solving my problem, anyone got any advice for me? - Keep in mind I’m new to this, I’m still in my first year of Computer Science and I don’t have as much time as I’d like to have to learn how to program well, but slowly I’m getting there, or at least I think I am.

Can anyone give me any good references/guides on how to learn OOP theory and how to apply it well, it doesn’t help that I’ve been told a few different things by a few different people. Both in university and out, I’m guessing it’s an ‘open’/debatable topic?

First of all:

should be changed to:

while(in.hasNextDouble()) {
     c1.add(in.nextDouble());
}

http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

What should this part do?

Do you want to divide the value in the list by 10?

For a quick intro to Java, see:

http://www.tutorialspoint.com/java/index.htm
http://www.tutorialspoint.com/java8/index.htm

For intros to more advanced topics, see:

http://www.tutorialspoint.com/java_technology_tutorials.htm

In general, I (having 4 years of experience with Java - also with enterprise technologies) suggest not to focus on Java too much. If it is required for your studies, learn it but don't get addicted to it. There are many downsides to Java that you can only see if you have experience with other languages and technologies and if you have managed to understand software engineering (including things like OOP) from a theoretical perspective. If you are currently doing your bachelor studies you won't see such things and I guess non of your instructors will tell you how bad Java can be for a lot of purposes. Learn it and learn it correctly and you will have a great foundation to learn other languages and also your studies should benefit from good Java coding skills.

For OOP see:

http://www.tutorialspoint.com/object_oriented_analysis_design/index.htm

NOTE: Those tutorials are just intros to cover some basics. Do some further reading using other resources.

1 Like

Is there a specific reason you've opted to use an iterator? One good reason for example is if you wanted to remove elements of your list while you're iterating. If you simply want to just iterate over the list, it's generally better form to just use java's for-each construct. For example..

for (Double item : c1) { foo(item); }

@Argon depends on the Java version you are using. For Java 7 the foreach loop construct is fine. In Java 8 you should go with streams and lambdas.

c1.stream().forEach(foo);

As much as I feel this approach is valid. Please keep him in the scope of his class if he asks for help as it will benefit him in his coursework.

That is all.

If the goal were to benefit him for coursework then no one should answer or provide troll answers such as my system.Exec('wc -l ' + fn) in some other java thread.

1 Like

I already gave him that advice. https://forum.teksyndicate.com/t/learning-how-to-program-in-java-using-arraylists/97083/2?u=ohban

Then @tsuser1729 introduced a best-practise approach and all I did, was to clarify that "best-practise" depends on the version of Java. In Java 8 you want to use streams to iterate through collections.

1 Like

Okay, so what I've decided to do is something more like:

while(i < 200){
    c1.add(in.nextInt());
    double temp = 0.00; 
    temp = c1.get(l);
    // some random arraylist here
    randomArray.add(temp);
    i++;
}

Would this sorta solution be okay?
Anyone see a way where I can improve this? - Doe it make a difference if I initialise (I'm British, I know in code, 9/10 of the time, everything uses American spelling, I know a lot of people would rather see the word initialize.) the temp variable inside or outside of the loop, I'd need to assign a new value to it every time the loop does loop, so does that make a difference?

What should this part do???

double temp = 0.00;
temp = c1.get(l);
// some random arraylist here
randomArray.add(temp);


original

Scanner in = new Scanner(System.in);
ArrayList c1 = new ArrayList<>();
Iterator c1itr = c1.iterator();

while(i < 200){
c1.add(in.nextInt());
double temp = 0.00;
temp = c1.get(l);
// some random arraylist here
randomArray.add(temp);
i++;
}


improved

Scanner in = new Scanner(System.in);
ArrayList c1 = new ArrayList<>();
Iterator c1itr = c1.iterator();

while(in.hasNextDouble()){
c1.add(in.hasNextDouble());
double temp = 0.00;
temp = c1.get(l);
// some random arraylist here
randomArray.add(temp);
}

Nevermind, I am sorry for asking for help, but after a lot of experimenting with things I'm not that confident with, I've got things to work, and work kinda okay, or at least I think, the point of the whole program is to use some of the tools that I've specified, like an ArrayList and a Scanner. But the whole point of this program was to read in data from a text file and then compute some of it and output it in the console, this was like a piece of coursework, but not really. By not really I mean this is a challenge piece of work that my lecturer has given us, I wanted to do it to try and challenge myself more than anything.

But thank you all for your help! :)

Also @Ohban thank you very much for the tip about using a regular expression, and how it's pointless and actually slower to use in this case scenario. But I knew that regular expressions are horrible to use in terms of readability, that's why when I have used them in the past, I have made a block comment above it, explaining what it does in detail.

I know .. All I was point out and this goes to the op too.. Is that be careful using.something your instructor hasn't talked about or shown.. Some of them are dicks .. Lol trust me I know this first hand.. XD