Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > New to Java

Reply
 
LinkBack (1) Thread Tools Display Modes
  #11 (permalink)  
Old 05-11-2008, 09:35 PM
Junior Member
 
Join Date: Nov 2008
Posts: 11
Fendaril is on a distinguished road
Default Re: New to Java/struggling with arrays

few ideas. Its one thing to know what the code does but a whole different issue of actually coding from scratch. I figured this is the best place to get ideas.
Reply With Quote
  #12 (permalink)  
Old 06-11-2008, 06:39 PM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: New to Java/struggling with arrays

Its good to write code that will help you learn different aspects of Java.

Do you feel you understand arrays enough now?

How are you with the different loops? for loop, while loop etc?

You could try to incorporate the use of an array and a for loop into one program.

Create an array of Names and use a for loop to print them all to the console.
Reply With Quote
  #13 (permalink)  
Old 06-11-2008, 09:02 PM
Junior Member
 
Join Date: Nov 2008
Posts: 11
Fendaril is on a distinguished road
Default Re: New to Java/struggling with arrays

Java Code:
public class Test1{
	public static void main(String[] args){
		String[] names={"bob","jill","frank","stewart","charlie"};
		for(String item: names){
			System.out.println("This name in the array is " + item);
		}
	}
}
Relating to your other question yes I understand the basic loops and array concepts now. The problem is I looked at the candidate program that was very recently posted here and saw very clever use of loops and arrays. That is what I am aiming toward.

I guess it will come with time.
Reply With Quote
  #14 (permalink)  
Old 07-11-2008, 11:39 AM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: New to Java/struggling with arrays

ah yeah I can see why you would think this code is more complicated when it actually isnt.

This is still a String array and for loop printing its content.

Array values can be stored like this:

Java Code:
String[] names={"bob","jill","frank","stewart","charlie"};
This is the same as:

Java Code:
String[] name = new String[5]
 
name[0] = "bob";
name[1] = "jill";
name[2] = "frank";
name[3] = "stewart";
name[4] = "charlie";
I think this concept first came into Java in version 4/5. They both work the same, its just down to personal preference to which one you use.

Java Code:
for(String item: names){
            System.out.println("This name in the array is " + item);
        }
The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each loop. Again its down to personal preference which one you use but keeping with the times makes it more readable.
Reply With Quote
  #15 (permalink)  
Old 07-11-2008, 01:45 PM
Junior Member
 
Join Date: Nov 2008
Posts: 11
Fendaril is on a distinguished road
Default Re: New to Java/struggling with arrays

so the reason I thought the candidate program was hard was because of the fact that he used a different method of array?

would it be more beneficial to create a standard for loop and have it iterate over the length while I print the arrays?

Last edited by Fendaril; 07-11-2008 at 01:49 PM.
Reply With Quote
  #16 (permalink)  
Old 07-11-2008, 03:58 PM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: New to Java/struggling with arrays

Yeah the reason it looked hard was because they used a different method of array.

As far as im aware, there is no difference in performance between the for and for-each loop. But if you want to print the content of an array I would get used to learning the for-each loop as its more convenient and no doubt you will encounter it alot in the future.
Reply With Quote
  #17 (permalink)  
Old 08-11-2008, 03:18 AM
Junior Member
 
Join Date: Nov 2008
Posts: 11
Fendaril is on a distinguished road
Default Re: New to Java/struggling with arrays

Yes the easy part is over.

Classes and objects seem to go through one ear and out the other...
Reply With Quote
  #18 (permalink)  
Old 08-11-2008, 11:16 AM
JavaPF's Avatar
Java Programmer
 
Join Date: May 2008
Location: Deep inside the Eclipse IDE
Posts: 172
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: New to Java/struggling with arrays

Quote:
Originally Posted by Fendaril View Post
Yes the easy part is over.

Classes and objects seem to go through one ear and out the other...
It will take time to sink in. Classes and Objects are the base of Object Oriented Programming.

Spend some time to read through the Sun Java tutorials about Classes and Objects. Even if you have read it 5 times before, read it again.

Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

You need to keep reading even if you don't understand. Each time it will gradually sink in more and more. Try to write code that will help you to understand. For example, try to pass a variable between 2 different classes..

I remember it took me a while to get my head around OOP too. The key really is just to keep writing code.
Reply With Quote
  #19 (permalink)  
Old 13-11-2008, 01:31 PM
Junior Member
 
Join Date: Nov 2008
Posts: 11
Fendaril is on a distinguished road
Default Re: New to Java/struggling with arrays

I havent done anything related to java in 2 days. I got so stressed out and just played games. I feel so incomplete so I am going to do something worthwhile.


Ok so I think I came upon a good project to start out with. I found out how to pass variables through different methods.

What if I passed variables through methods like 'addition(i,j)' while taking input from the user. It does require me to jump a head a little bit but a calculator is something that is very easy to make. I could use the beautiful if-else ladder. The process I have to take to make it is a bit harder then in c++. Not comparing or anything.

Last edited by Fendaril; 13-11-2008 at 01:37 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.javaprogrammingforums.com/new-java/150-new-java-struggling-arrays.html
Posted By For Type Date
Java Programming Forums - JavaProgrammingForums.com This thread Refback 04-11-2008 03:04 PM

Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse shortcut keys ejb3 quartz java convert double to binary java forum java forums java jtextarea bold java jtextarea color java jtextarea font java jtextarea font size java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed javapf javaprogrammingforums jtextarea bold jtextarea font jtextarea font color jtextarea font size programing forums programming forums reset jcombobox saaj0008 saaj0008: bad response; bad request saaj0008: bad response; not found severe: saaj0008 severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soap java.security.privilegedactionexception soapexceptionimpl: bad endpoint type textpad java

All times are GMT. The time now is 12:07 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.