<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CM &#187; programming</title>
	<atom:link href="http://corymathews.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://corymathews.com</link>
	<description>Things I should remember and then some...</description>
	<lastBuildDate>Thu, 26 Jan 2012 16:27:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Linked Lists in Java Part 2</title>
		<link>http://corymathews.com/linked-lists-in-java-part-2/</link>
		<comments>http://corymathews.com/linked-lists-in-java-part-2/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 17:05:14 +0000</pubDate>
		<dc:creator>CoryMathews</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lists]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://corymathews.com/?p=63</guid>
		<description><![CDATA[In Part One we learned the basics of using the Linked list class in java. I showed how to create, add, edit, and remove items from the list. Now I am going to show you how to use the following functions: .toString(), .equal(), .hashCode(), .isEmpty(), .subList(), .contains(), For all my examples I am using the [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://corymathews.com/index.php?p=61">Part One</a> we learned the basics of using the Linked list class in java. I showed how to create, add, edit, and remove items from the list.</p>
<p>Now I am going to show you how to use the following functions:</p>
<pre class="prettyprint linenumstrigger linenums">.toString(), .equal(), .hashCode(), .isEmpty(), .subList(), .contains(),</pre>
<p>For all my examples I am using the list MyList with the contents [1, 2, 3].</p>
<p><strong>.toString() </strong></p>
<p>This function is an excellent debugging statement, or just a simple very quick way to see everything in your list. Its simple to use:</p>
<pre class="prettyprint linenumstrigger linenums">
MyList.toString();
//or to print out the list
System.out.println(MyList.toString());
</pre>
<p>This would print out:</p>
<pre class="prettyprint linenumstrigger linenums">[1, 2, 3]</pre>
<p><strong>.equal()</strong></p>
<p>This method does exactly what you would think it would. It compares two lists to see if they are equal. For two lists to be equal they must have all the same items, and they must be in the same order. So in other words one must be a deep copy of the other. Here is a little example using .equal() and .toString().</p>
<pre class="prettyprint linenumstrigger linenums">
MyList.add(&quot;1&quot;);
MyList.add(&quot;2&quot;);
MyList.add(&quot;3&quot;);
MyList2.add(&quot;1&quot;);
MyList2.add(&quot;2&quot;);
MyList2.add(&quot;3&quot;);
System.out.println(MyList.toString());
System.out.println(MyList2.toString());
System.out.println(MyList.equals(MyList2));

MyList2.remove(1);
MyList2.add(&quot;2&quot;);
System.out.println(MyList.toString());
System.out.println(MyList2.toString());
System.out.println(MyList.equals(MyList2));
</pre>
<p>This would print out</p>
<pre class="prettyprint linenumstrigger linenums">[1, 2, 3]
[1, 2, 3]
true
[1, 2, 3]
[1, 3, 2]
false</pre>
<p><strong>.hashCode()</strong></p>
<p>.hashCode() uses an algorithm to generate a number based on the list. This can be used for encryption or anything that you need a simple reliable number to represent a list. Sun provides the algorithm for generating the number and here it is.</p>
<pre class="prettyprint linenumstrigger linenums">
  hashCode = 1;
  Iterator i = list.iterator();
  while (i.hasNext()) {
      Object obj = i.next();
      hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  }
</pre>
<p>You don&#8217;t actually need to know its algorithm but its always nice to see what exactly it is doing. so Now here is a short example using the hasCode(). I am using the resulting values from the .equal() section so as a reminder MyList contains [1,2,3] and MyList2 contains [1,3,2] and they are all strings.</p>
<pre class="prettyprint linenumstrigger linenums">
System.out.println(MyList.hashCode());
System.out.println(MyList.hashCode());
</pre>
<p>This would output</p>
<pre class="prettyprint linenumstrigger linenums">
78481
78511</pre>
<p>As you can see these two lists produce different hash codes. So even though the lists contain the same strings they still produce different hash codes.</p>
<p><strong>.isEmpty()</strong></p>
<p>This is a boolean method that does exactly what it says. It return true or false depending if the list contains anything.</p>
<pre class="prettyprint linenumstrigger linenums">System.out.println(MyList.isEmpty());</pre>
<p>would output</p>
<pre class="prettyprint linenumstrigger linenums">false</pre>
<p><strong>.subList()</strong></p>
<p>subList allows you to return a shallow copy of part of the list. So changing the values of the subList will change the values of the original list. So for example MyList contains [1,2,3] the example code:</p>
<pre class="prettyprint linenumstrigger linenums">System.out.println(MyList.subList(0,3));
System.out.println(MyList.subList(1,2));
System.out.println(MyList.subList(2,3));
System.out.println(MyList.subList(2,6));</pre>
<p>displays</p>
<pre class="prettyprint linenumstrigger linenums">[1, 2, 3]
[2]
[3]
Exception in thread &quot;main&quot; java.lang.IndexOutOfBoundsException: toIndex = 6
at java.util.SubList.(AbstractList.java:705)
at java.util.AbstractList.subList(AbstractList.java:570)
at LinkedListTest.main(LinkedListTest.java:37)

Since my list is only 3 items long you can see that trying to go to 6 gives and IndexOutOfBoundsException and crashes. But looking at the 3 previous calls, I can get the entire list as a sublist, kind of pointless but it can be done. I can also take just specific elements as you see in the middle 2 examples.

We can also combine subList and clear to imitate the method removeRange which for whatever reason I could never get to work. The code:

&lt;code&gt;MyList.subList(0, 2).clear();
System.out.println(MyList.size());
System.out.println(MyList.toString());</pre>
<p>would change our list and output</p>
<pre class="prettyprint linenumstrigger linenums">1
[3]</pre>
<p><strong>.contains()</strong></p>
<p>This is a very useful method. It return a true or false depending if the item you are looking for is in the list or not. So if i was to run the code</p>
<pre class="prettyprint linenumstrigger linenums">
System.out.println(MyList.contains(&quot;1&quot;));
System.out.println(MyList.contains(&quot;0&quot;));</pre>
<p>It would print out</p>
<pre class="prettyprint linenumstrigger linenums">true
false</pre>
<p>Thats all for now, as always here is an example source with all the explained methods being used.</p>
<pre class="prettyprint linenumstrigger linenums">
import java.util.*;
public class LinkedListTest
{
  public static void main(String[] args)
  {
	  LinkedList MyList = new LinkedList();
	  LinkedList MyList2 = new LinkedList();

	  MyList.add(&quot;1&quot;);
	  MyList.add(&quot;2&quot;);
	  MyList.add(&quot;3&quot;);
	  MyList2.add(&quot;1&quot;);
	  MyList2.add(&quot;2&quot;);
	  MyList2.add(&quot;3&quot;);
	  System.out.println(MyList.toString());
	  System.out.println(MyList2.toString());
	  System.out.println(MyList.equals(MyList2));

	  MyList2.remove(1);
	  MyList2.add(&quot;2&quot;);
	  System.out.println(MyList.toString());
	  System.out.println(MyList2.toString());
	  System.out.println(MyList.equals(MyList2));

	  System.out.println(MyList.hashCode());
	  System.out.println(MyList2.hashCode());

	  System.out.println(MyList.isEmpty());
	  System.out.println(MyList.subList(0,3));
	  System.out.println(MyList.subList(1,2));
	  System.out.println(MyList.subList(2,3));
	  MyList.subList(0, 2).clear();
	  System.out.println(MyList.size());
	  System.out.println(MyList.toString());
	  MyList.add(&quot;2&quot;);
	  MyList.add(&quot;1&quot;);

	  System.out.println(MyList.contains(&quot;1&quot;));
	  System.out.println(MyList.contains(&quot;0&quot;));
  }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://corymathews.com/linked-lists-in-java-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linked Lists in Java Part 1</title>
		<link>http://corymathews.com/linked-lists-in-java-part-1/</link>
		<comments>http://corymathews.com/linked-lists-in-java-part-1/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 01:27:17 +0000</pubDate>
		<dc:creator>CoryMathews</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lists]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://corymathews.com/?p=61</guid>
		<description><![CDATA[Recently I have been playing around with Java&#8217;s LinkedList class. This is a very usefull class that is included in java.util.LinkedList. The LinkedList Class allows you to implement a linked list (what a surprise) with a simple class declaration. I am going to explain how to create and use the class, as well as a [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been playing around with Java&#8217;s LinkedList class. This is a very usefull class that is included in java.util.LinkedList.</p>
<p>The LinkedList Class allows you to implement a linked list (what a surprise) with a simple class declaration. I am going to explain how to create and use the class, as well as a few tips I have picked up while playing around with the class.<br />
<span id="more-61"></span><br />
<strong>In Part 1 I will cover:</strong><br />
Adding information<br />
The basics of accessing it<br />
Then removing it.</p>
<p><a href="http://corymathews.com/index.php?p=63">View Part 2 Here</a></p>
<p>First things first we need to include the class.</p>
<pre name="code" class="java">import java.util.LinkedList;
//or the lazy mans way
import java.util.*;</pre>
<p>Now on to creating an instance of the class.</p>
<pre name="code" class="java">LinkedList MyList = new LinkedList();
//or more specifically define its type.
LinkedList&lt; String&gt; MyList = new LinkedList&lt; String&gt;();</pre>
<p>Note: make sure to remove the space before String in both cases. WordPress would not display the &lt; String&gt; without the space.<br />
Otherwise pretty simple, it&#8217;s just like any other class. I am going to call the list MyList.</p>
<p><strong>Adding to the List:</strong><br />
The first thing you would want to do is to add to the list. This can be done in 1 of 4 ways. The first and most basic is</p>
<pre name="code" class="java">MyList.add("1");
//or
My List.add(varName);</pre>
<p>This simply adds an item to the <strong>end</strong> of a list.<br />
We can then expand on this and declare what spot in the array we want to add to. For example if I want to add it to the front (I will show a better way in a minute).</p>
<pre name="code" class="java">MyList.add(0, "2");
//or
My List.add(0, varName);</pre>
<p>The 0 just states that we want it as the first element. But you could substitue zero for any number within the list. Note that the destination spot must be within the size of the array. For instance right now after adding 1 and 2 from our two previous examples we have a list with size of 2. We could not insert into spot 4, an IndexOutOfBoundsException would be thrown and if not caught our program would crash.</p>
<p>The next two ways to add are pretty self documented. They are the addFirst() and addLast() functions. They can be used just like the add() method except the position to add to has already been set.</p>
<pre name="code" class="java">MyList.addFirst("3");
MyList.addLast("4");</pre>
<p>There is also a function to add everything from a collection called .addAll() but I am not going to get into this because I do not have enough practice with collections to explain. You can find out more about using this method <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html#addAll(int, java.util.Collection)">here</a>.</p>
<p><strong>Basics of Accessing Items</strong></p>
<p>Getting the Size:<br />
The first thing is a simple method to return the size of a list. The following would print the size of our list using the .size() method.</p>
<pre name="code" class="java">System.out.println(MyList.size());</pre>
<p>Getting the value of a specific spot.<br />
using the .get() method we can get the value of any index in our list. If the index is not in the list a IndexOutOfBoundsException is thrown. Using this method does NOT remove the item from the list itself.</p>
<pre name="code" class="java">System.out.println(MyList.get(0));
//or to store the value first
temp = MyList.get(0);
System.out.println(temp);</pre>
<p>This prints out the first item in our list. But does NOT remove it from the list.</p>
<p><em>I keep stressing that it does not remove it from the list because a couple tutorials I read when first learning the class, told me that it does remove the item. So I just want to make it clear to save you the trouble.</em></p>
<p>Just like with adding there is a getFirst() and getLast() that do exactly what they say.</p>
<p><strong>Removing an Item:</strong><br />
So now we can add items and access items but we need to be able to get rid of the items. This is where the function .remove(), .removeFirst() and .removeLast() come into play. They work just like the add, and get functions. So we could remove our 4 items from our list using:</p>
<pre name="code" class="java">MyList.removeLast();
MyList.remove(0);
MyList.removeFirst();
MyList.removeFirst();</pre>
<p>Of course the order here doesn&#8217;t matter I just wanted to show the 3 different ways to remove an item.</p>
<p>Here is a little useless example program using the LinkedList Class to see everything put together.</p>
<pre name="code" class="java">import java.util.*;public class LinkedListTest
{
public static void main(String[] args)
{
int pause;
String temp;
LinkedList MyList = new LinkedList();
MyList.add("1");
MyList.add(0, "2");
MyList.addFirst("3");
MyList.addLast("4");
System.out.println("Size: "+MyList.size());
System.out.println("Element 0: "+MyList.get(0));
temp = MyList.get(1);
System.out.println("Element 1: "+temp);
System.out.println("Size :"+MyList.size());
System.out.println("First :"+MyList.getFirst());
System.out.println("Last :"+MyList.getLast());
System.out.println("Size yet again: "+MyList.size());
MyList.removeFirst();
System.out.println("Size yet again: "+MyList.size());
MyList.removeLast();
MyList.remove(0);
MyList.removeFirst();
System.out.println("Empty Size: "+MyList.size());for(int x=0; x&lt;100; x++) {
MyList.add(""+x);
}
System.out.println("Loop Size :"+MyList.size());
for(int x=100; x&gt;0; x--) {
MyList.removeLast();
}
System.out.println("Empty Size: "+MyList.size());
}
}
</pre>
<p><a href="http://corymathews.com/index.php?p=63">View Part 2 Here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://corymathews.com/linked-lists-in-java-part-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Command Line Arguments</title>
		<link>http://corymathews.com/command-line-arguments/</link>
		<comments>http://corymathews.com/command-line-arguments/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 18:08:20 +0000</pubDate>
		<dc:creator>CoryMathews</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://corymathews.com/?p=21</guid>
		<description><![CDATA[Recently in a couple class projects I have needed to use command line arguments, and the first time dealing with these can be a little confusing. So here is four short programs that simply output these arguments. What I mean by this is that I need to accept a string while I am calling the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently in a couple class projects I have needed to use command line arguments, and the first time dealing with these can be a little confusing. So here is four short programs that simply output these arguments.<br />
<span id="more-21"></span><br />
What I mean by this is that I need to accept a string while I am calling the program name. For example I am running my program called printString and I want it to display the text hello. So I run it like so</p>
<pre name="code" class="java">./printString.c hello</pre>
<p>This would make my program print hello. Now I will show how its done. Its basically just like passing parameters to a normal function, except its a character string, and there is no limit to how many you can add, note that these examples are only for 1 parameter. A simple loop would allow you to output them all.</p>
<p>Also For the c and c++ example the argc variable is how many parameters you passed in. The reason I use 1(one) and not 0 is because arg[0] is the name of the file itself.</p>
<p>In Java</p>
<pre name="code" class="java">
public class printString{
  public static void main (String[] args) {
    System.out.println(args[0])
  }
}
</pre>
<p>In c</p>
<pre name="code" class="c">nt main(int argc, char **argv)
{
strcpy(ip_addr_dot, argv[1]);
printf(" %s", ip_addr_dot);
return 0;
}</pre>
<p>In c++</p>
<pre name="code" class="c++">#include&lt;iostream&gt;
using namespace std;
int main(int argc, char **argv)
{
cout &lt;&lt; argv[1];
return 0;
}</pre>
<p>In Python</p>
<pre name="code" class="c">import sys
for arg in sys.argv:
print arg</pre>
<p><em>Note the there is an indent in front of print.</em></p>
<p>All of these short programs output the word hello and then end.</p>
]]></content:encoded>
			<wfw:commentRss>http://corymathews.com/command-line-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

