torsdag 31 maj 2012

ArrayList

Lecture 17 - ArrayList.

ArrayList is an object. The actual size equals (almost all the times) the effective size.
import java.util.*;

ArrayList<String> slist = new ArrayList<String>( ); //an arraylist of strings

An arraylist holds objects. You can't use primitve types as int, double etc. However you can use the wrapperclass; Integer, Double, Boolean, Character. Notice: They are immuntable, just like Strings, which means that you can't change them. You have to create a new one and override the old one.

But when you write your code you can write like this:

ArrayList<Integer> ilist = new <Integer>( );
   int x = 5;
   ilist.add(x);
   int z = ilist.get(0);

2 dimension array

type [ ] [ ] name = new type [size][size];

int [ ] [ ] twoD = new int [2][3];

for (int i =0; i < 2; i++){
   for (int j =0; j < 3; j++){
     twoD[i][j] = 1;
}
}
     

++, post and pre increment

++ is a method and it returns something.

x ++; is called post increment and it adds 1 after the value is returned.

++x; is called pre  increment and adds 1 and then returns the new value.

Swapping values using arrays

private void SwapElements (int [ ] arrayName; int position1, int position2){
   int temporary = arrayName[position1];
   arrayName[position1] = arrayName[position2];
   arrayName[position2] = temporary;
}

You have to send (to the method) the actual array and the positions of the elements in it that you want to swap. If you send only the elements (int arrayName[1], int arrayName[3]) you are only sending a COPIE and it will change nothing in the array itself.

Arrays

Lektion 16 handlar bland annat om Arrayer.

An array is ordered and homogeneous. You are creating space for the object. An array is passed by reference (as an object) and NOT as a copie.

type[ ] name = new type[size];

There is a difference between acutal size (what you declare) and effective size (what you really are using).

Ex. asking the user for the values in the array
   int[ ] myArray = new int[5];
      for (int i = 0; i < 5; i++){
           myArray[i] = readInt("Value?:");
       }

Ex. Ask the user for the length of the array
  int maxLength = readInt("Max sixe of the array:");
  int[ ] myArray = new int [maxLength];

Ex. To keep track of where you are putting your values
   private static final int SENTINEL = -1;
   int numberOfScores = 0;
      for (int i = 0; i < maxLength; i ++){
         myArray[i] = readInt ("Next score:");
         if (myArray[i] == SENTINEL) break;
            numberOfScores++;
   

onsdag 30 maj 2012

Copying a file

Här blev det lite snurrigt men enkom för att jag trodde att jag gjorde ett program för att skriva filer men egentligen var det ju att kopiera filer... skyller på sommarvärmen. Nu är det klart i alla fall och funkar prima. Kodandet fortsätter...

fredag 25 maj 2012

Open and read textfile

Äntligen löste lyckades jag med uppgiften. Jag raderade allt från igår och började om på nytt.


/*
 * Date: 2012-05-25
 * Making a program for open and reading a textfile
 */
package fileDemo;
import java.util.Scanner;

public class FileDemo {

public static void main (String[] args){

//create Fileobject
File file1 = new File();

//get filename
System.out.println("Filens namn: ");
Scanner in = new Scanner (System.in);
String filename = in.nextLine();


//invoke methods on that object
file1.openFile(filename);
file1.readFile();
}
}
package fileDemo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class File {

//variables
String filename;
BufferedReader rd = null;
//methods
        public BufferedReader openFile(String newFilename){

filename = newFilename;
System.out.println(newFilename);

while (rd == null){
try{
rd = new BufferedReader (new FileReader(newFilename));
}
catch (IOException ex){
System.out.println("This file does not exist.");
break;
}
}
return rd;
}
public void readFile(){
try{
while (true){
String line = rd.readLine();
if (line == null) break;
System.out.println("Read line: [" + line + "]");
}
rd.close();
}
catch (IOException ex){
System.out.println("There is nothing on this file to read.");
}
}
}