Search Results

Search found 1459 results on 59 pages for 'arraylist'.

Page 3/59 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Android ArrayList Calendar add and get is different

    - by Raku ShinShou
    I put increment date of the calendar and add into arraylist, but when i loop them out, the date is all same. Calendar a = Calendar.getInstance(); ArrayList<Calendar> b = new ArrayList<Calendar>(); for (int i=0; i<3; i++) { a.add(Calendar.DATE, i>0 ? 1 : 0); b.add(a); } for (int i=0; i<b.size(); i++){ Log.d("xxx", "test=" + b.get(i)); } suppose i wan 10/11/2012, 11/11/2012, 12/11/2012 but it come out like 12/11/2012, 12/11/2012, 12/11/2012 Anyone know how to solve this?

    Read the article

  • Java ArrayList<Double> IndexOutOfBoundsException Problem

    - by Sebastian Bechtel
    Hello, I've got a Problem with ArrayList. I need it to store a result. Because I want to start with element n I tryed to give the ArrayList a capacity with ensureCapacity(n+1) to use set(n,x) but I get an IndexOutOfBoundsException. I tryed to store n add(x) before the use of set and this works. So I'd like to know why it doesn't work on my way and how to solve this because put n times a add(x) isn't a good style ;-) Best regards, Sebastian

    Read the article

  • Return ArrayList from .net web service to android application using kSoap

    - by benjamin schultz
    My web service is returning quite a bit of data, so i'm storing it in an arraylist and returning that to my application. I don't know whether or not this is the preferred method or not. When the arraylist is returned and displayed in my application, it also displays the arraylists "anyType" section. I'm new to this whole process so i'm sure i'm missing something somewhere or going about this incorrectly. Can anyone point me in the right direction please?

    Read the article

  • ArrayList in java

    - by fatih-yilmaz
    ArrayList<String> veri1 = new ArrayList<String>(); String[] veri2 = {"Fatih", "Ferhat", "Furkan"}; How can i add "veri2" to "veri1" like one element ? I mean if i call veri.get(0), it returns veri2.. Regards.

    Read the article

  • Add elements to Arraylist and it replaces all previous elements in Java

    - by pie154
    I am adding elements to a ArrayList and it adds the first one correctly but then when I add any subsequent elements it wipes replaces the other elements with the value form the most recently added and adds a new element to the ArrayList. I ran test using arraylist and ints and even another created class and it worked perfectly but soemthing about the custon class i am using here causes problems. The code for the array list is public static void main(String args[]){ List<BasicEvent> list = new ArrayList<BasicEvent>(); list.add(new BasicEvent("Basic", "Door", 9, 4444, new Date(12,04,2010), new Time(12,04,21), 1, 0.98, 0)); list.add(new BasicEvent("Composite", "Door", 125, 4444, new Date(12,04,2010), new Time(12,04,20), 1, 0.98, 1)); list.add(new BasicEvent("Basic", "Door", 105, 88, new Date(12,04,2010), new Time(12,05,23), 1, 0.98, 0)); list.add(new BasicEvent("Basic", "Door", 125, 12, new Date(12,04,2010), new Time(12,05,28), 1, 0.98, 1)); list.add(new BasicEvent("Basic", "Door", 129, 25, new Date(12,04,2010), new Time(12,05,30), 1, 0.98, 0)); list.add(new BasicEvent("Basic", "Door", 125, 63, new Date(12,04,2010), new Time(12,04,20), 1, 0.98, 1)); list.add(new BasicEvent("Basic", "Detect", 127, 9, new Date(12,04,2010), new Time(12,05,29), 1, 0.98, -1)); for(int i=0;i<list.size();i++) {System.out.println("list a poition " + i + " is " + BasicEvent.basicToString(list.get(i)));} And the code for the custom class basicEvent is public class BasicEvent { public static String Level; public static String EType; public static double xPos; public static double yPos; public static Date date; public static Time time; public static double Rlb; public static double Sig; public static int Reserved; public BasicEvent(String L, String E, double X, double Y, Date D, Time T, double R, double S, int Res){ Level = L; EType = E; xPos = X; yPos = Y; date = D; time = T; Rlb = R; Sig = S; Reserved = Res; }; public static String basicToString(BasicEvent bse){ String out = bse.getLevel() + ";" + bse.getEtype() + ";" + bse.getxPos() + ";" + bse.getyPos() + ";" + bse.getDate().dateAsString() + ";" + bse.getTime().timeAsString() + ";" + bse.getRlb() + ";" + bse.getSig() + ";" + bse.getReserved(); return out; }

    Read the article

  • How do I sort an ArrayList lexicographically?

    - by Jake
    I am trying to sort an ArrayList of Strings that represent card values. So, some cards contain letters ("King") and some contain Strings containing only a number ("7"). I know to use Collections.sort, but it only sorts Strings that contain letters. How do I get the ArrayList to be sorted by number as well as alphabetically?

    Read the article

  • Write Parcelable for ArrayList<String []> in android?

    - by sugan.s
    I just created model with String array and array list of string array.Like this public class LookUpModel implements Parcelable { private String [] lookup_header; private ArrayList<String []> loookup_values; public void writeToParcel(Parcel dest, int flags) { dest.writeStringArray(getLookup_header()); }; } I have implemented parcelbale then write for String [] but how to do for the ArrayList<String []> and that values need to pass to another activity.Thanks in advance.

    Read the article

  • ArrayList of my objects, indexOf problem

    - by majtits
    Hello! I have problem with Java's ArrayList. I've created an Object, that contains two atributes, x and y. Now I've loaded some object in my ArrayList. Problem is that I don't know how to find index of some object with x atribute I'm searching. Is there any way to do this?

    Read the article

  • How to "cast" from generic List<> to ArrayList

    - by Michael Freidgeim
    We are writing new code using generic List<> , e.g. List<MyClass>.   However we have legacy functions, that are expect ArrayList as a parameter. It is a second time, when I and my colleague asked, how to "cast" generic List<MyClass> to ArrayList. The answer is simple- just use ArrayList constructor with ICollection parameter. Note that it is not real cast, it  copies  references to ArrayList. var list=new List<MyClass>(); //Fill list items ArrayList al=new ArrayList(list);//"cast"-

    Read the article

  • How to check and match the possible combinations of arraylist elements

    - by Jessy
    String [] A = {"High","Medium","Low"}; String [] B = {"High","Medium","Low"}; String [] C = {"High","Medium","Low"}; String [] D = {"High","Medium","Low"}; String [] E = {"High","Medium","Low"}; String [] F = {"High","Medium","Low"}; JComboBox Ai = new JComboBox(A); JComboBox Bi = new JComboBox(B); JComboBox Ci = new JComboBox(C); JComboBox Di = new JComboBox(C); JComboBox Ei = new JComboBox(E); JComboBox Fi = new JComboBox(F); .... //add the user choice in arrayList ArrayList<String> a = new ArrayList<String>(); a.add((String) Ai.getSelectedItem()); a.add((String) Bi.getSelectedItem()); a.add((String) Ci.getSelectedItem()); a.add((String) Di.getSelectedItem()); a.add((String) Ei.getSelectedItem()); a.add((String) Fi.getSelectedItem()); Scenario: On each comboBox, user need to choose one, which mean there are 6 choices at the end. There are 6*5*4*3*2*1 = 720 possible combinations of choices made by the user. What is the best way to check and match the user choice without writing the 720 else if ? e.g. if(Ai=="High" && Bi=="High" && Ci=="Low" && Di=="High" && Ei=="Low" && Fi=="Medium") { System.out.println("Good Choice"); } Thank you.

    Read the article

  • ArrayList in Java, adding code to methods

    - by yaz
    Why am I getting a compile time error for the two method headers at the end of my code in my class ArrayListTest? ArrayListTest: http://pastebin.com/dUHn9vPr Student: http://pastebin.com/3Vz1Aytr I have a compiler error at the two lines: delete(CS242, s3) replace(CS242, s, s4); When I try to run the code it states: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method replace(ArrayList<Student>, Student, Student)in the type ArrayListTest is not applicable for the arguments (List<Student>, Student, Student) The method delete(ArrayList<Student>, Student) in the type ArrayListTest is not applicable for the arguments (List<Student>, Student) at ArrayListTest.main(ArrayListTest.java:54) I fixed the compile time errors since I use Eclipse and Eclipse gives options of code you can use to fix the compile time error. I chose to "Change method to 'replace(ArrayList, Student, Student)' to 'replace(List, Student, Student)' Although it fixed that compile time error, I don't understand why I was getting a compile time error to begin with and why that effectively fixed the error I don't really know what missing code I need to write to correct these two methods below: public static void replace(List<Student> cS242, Student oldItem, Student newItem) { public static void delete(List<Student> cS242, Student target){

    Read the article

  • Searching in an Arraylist

    - by Puchatek
    Currently I have two classes. A Classroom class and a School class. I would like to write a method in the School class public void showClassRoomDetails which would find the classroom details by only using the teacherName. e.g. teacherName = Daniel className = Science teacherName = Bob className = Maths so when I input Bob, it would print out Bob and Maths many, thanks public class Classroom { private String classRoomName; private String teacherName; public void setClassRoomName(String newClassRoomName) { classRoomName = newClassRoomName; } public String returnClassRoomName() { return classRoomName; } public void setTeacherName(String newTeacherName) { teacherName = newTeacherName; } public String returnTeacherName() { return teacherName; } } import java.util.ArrayList; public class School { private ArrayList<Classroom> classrooms; private String classRoomName; private String teacherName; public School() { classrooms = new ArrayList<Classroom>(); } public void addClassRoom(Classroom newClassRoom, String theClassRoomName) { classrooms.add(newClassRoom); classRoomName = theClassRoomName; } public void addTeacherToClassRoom(int classroomId, String TeacherName) { if (classroomId < classrooms.size() ) { classrooms.get(classroomId).setTeacherName(TeacherName); } } public void showClassRoomDetails { //loop System.out.println(returnClassRoomName); System.out.println(returnTeacherName); } }

    Read the article

  • Java - Removing duplicates in an ArrayList

    - by Will
    I'm working on a program that uses an ArrayList to store Strings. The program prompts the user with a menu and allows the user to choose an operation to perform. Such operations are adding Strings to the List, printing the entries etc. What I want to be able to do is create a method called removeDuplicates().This method will search the ArrayList and remove any duplicated values. I want to leave one instance of the duplicated value(s) within the list. I also want this method to return the total number of duplicates removed. I've been trying to use nested loops to accomplish this but I've been running into trouble because when entries get deleted, the indexing of the ArrayList gets altered and things don't work as they should. I know conceptually what I need to do but I'm having trouble implementing this idea in code. Here is some pseudo code: start with first entry; check each subsequent entry in the list and see if it matches the first entry; remove each subsequent entry in the list that matches the first entry; after all entries have been examined, move on to the second entry; check each entry in the list and see if it matches the second entry; remove each entry in the list that matches the second entry; repeat for entry in the list Here's the code I have so far: public int removeDuplicates() { int duplicates = 0; for ( int i = 0; i < strings.size(); i++ ) { for ( int j = 0; j < strings.size(); j++ ) { if ( i == j ) { // i & j refer to same entry so do nothing } else if ( strings.get( j ).equals( strings.get( i ) ) ) { strings.remove( j ); duplicates++; } } } return duplicates; }

    Read the article

  • Java - problems iterating through an ArrayList

    - by cc0
    Ok so I have an ArrayList (arrBok), which is full of book objects (the code is in Norwegian, so pay no attention to that please). I want to make a public method which iterates through all the objects in the arraylist. When I execute the code, it just seems to run in an infinite loop, not producing any return values. Here is the relevant (I hope, because there are a couple of other classes involved) part of the code; public String listAll() { itr = arrBok.iterator(); while (itr.hasNext()) { i++; } return "lol"; } This code does nothing useful, but I just want to see if it can iterate through it successfully. What I have tried so far; Tested if the bokArr (arraylist) is empty, which it's not. It has 4 objects inside of it. Return the toString() method of the itr, with the following result; java.util.AbstractList$Itr@173a10f // <-- not sure if this would be relevant to anything return itr.next().toString(); <-- // which seems to return the first object in the array, does that make sense?

    Read the article

  • Java: Generating distribution of values in an ArrayList

    - by Matt
    Hi all, I have a sorted ArrayList of values. I would like to get the distribution of the values. For example: Say I have 500 values, ranging from 1-100. I want to break them up into groups, say 10 groups: values 1-10, 11-20, 21-30, etc... I want the counts of each of the 500 values that fall into each category. For example, 5 of the 500 are valued at 1-10, 20 between 11-20, etc... However, I do not know the ranges of values in my ArrayList, it could be ranging from 1-30 or 1-200, but I want to break it up into, for example, 10 groups. Does anyone know how to do this?

    Read the article

  • Objects in Java ArrayList don't get updated.

    - by Sbm007
    This is going to be a very long post, hopefully you can understand what I'm talking about and I appreciate any help. Thanks Basically, I've created a personal, non-commercial project (which I don't plan to release) that can read ZIP and RAR files. It can only read the contents in the archive, the folders inside, the files inside the folders and its properties (such as last modified date, last modified time, CRC checksum, uncompressed size, compressed size and file name). It can't extract files either, so it's really a ZIP/RAR viewer if you may. Anyway that's slightly irrelevant to my problem but I thought I'd give you some background info. Now for my problem: I can successfully list all the folders and files inside a ZIP archive, so now I want to take that raw input and link it together in some useful way. I made 2 classes: ArchiveFile (represents a file inside a ZIP) and ArchiveFolder (represents a folder inside a ZIP). They both have some useful methods such as getLastModifiedDate, getName, getPath and so on. But the difference is that ArchiveFolder can hold an ArrayList of ArchiveFile's and additional ArchiveFolder's (think of this as files and folders inside a folder). Now I want to populate my raw input into one root ArchiveFolder, which will have all the files in the root dir of the ZIP in the ArchiveFile's ArrayList and any additional folders in the root dir of the ZIP in the ArchiveFolder's ArrayList (and this process can continue on like this like a chain reaction (more files/folders in that ArchiveFolder etc etc). So I came up with the following code: while (archive.hasMore()) { String path = ""; ArchiveFolder current = root; String[] contents = archive.getName().split("/"); for (int x = 0; x < contents.length; ++x) { if (x == (contents.length - 1) && !archive.getName().endsWith("/")) { // If on last item and item is a file path += contents[x]; // Update final path ArchiveFile file = new ArchiveFile(path, contents[x], archive.getUncompressedSize(), archive.getCompressedSize(), archive.getModifiedTime(), archive.getModifiedDate(), archive.getCRC()); current.addFile(file); // Create and add the file to the current ArchiveFolder } else if (x == (contents.length - 1)) { // Else if we are on last item and it is a folder path += contents[x] + "/"; // Update final path ArchiveFolder folder = new ArchiveFolder(path, contents[x], archive.getModifiedTime(), archive.getModifiedDate()); current.addFolder(folder); // Create and add this folder to the current ArchiveFile } else { // Else if we are still traversing through the path path += contents[x] + "/"; // Update path ArchiveFolder folder = new ArchiveFolder(path, contents[x]); current.addFolder(folder); // Create and add folder (remember we do not know the modified date/time as all we know is the path, so we can deduce the name only) current = folder; // Update current ArchiveFolder to the newly created one for the next iteration of the for loop } } archive.getNext(); } Assume that root is the root ArchiveFolder (initially empty). And that archive.getName() returns the name of the current file OR folder in the following fashion: file.txt or folder1/file2.txt or folder4/folder2/ (this is a empty folder) etc. So basically the relative path from the root of the ZIP archive. Please read through the comments in the above code to familiarize yourself with it. Also assume that the addFolder method in an ArchiveFile, only adds the folder if it doesn't exist already (so there are no multiple folders) and it also updates the time and date of an existing folder if it is blank (ie it was a intermediate folder we only knew the name of, but now we know its details). The code for addFolder is (pretty self-explanitory): public void addFolder(ArchiveFolder folder) { int loc = folders.indexOf(folder); // folders is the ArrayList containing ArchiveFolder's if (loc == -1) { folders.add(folder); } else { ArchiveFolder real = folders.get(loc); if (real.time == null) { real.setTime(folder.getTime()); real.setDate(folder.getDate()); } } } So I can't see anything wrong with the code, it works and after finishing, the root ArchiveFolder contains all the files in the root of the ZIP as I want it to, and it contains all the direcories in the root folder as I want it to. So you'd think it works as expected, but no the ArchiveFolder's in the root folder don't contain the data inside those 'child' folders, it's just a blank folder with no additional files and folders (while it does really contain some more files/folders when viewed in WinZip). After debugging using Eclipse, the for loop does iterate through all the files (even those not included above), so this led me to believe that there is a problem with this line of the code: current = folder; What it does is, it updates the current folder (used as an intermediate by the loop) to the newly added folder. I thought Java passed by reference and thus all new operations and new additions in future ArchiveFile's and ArchiveFolder's are automatically updated, and parent ArchiveFolder's will be updated accordingly. But that does not appear to be the case? I know this is a long ass post and I really hope anyone can help me out with this. Thanks in advance.

    Read the article

  • Remove duplicates from a sorted ArrayList while keeping some elements from the duplicates

    - by js82
    Okay at first I thought this would be pretty straightforward. But I can't think of an efficient way to solve this. I figured a brute force way to solve this but that's not very elegant. I have an ArrayList. Contacts is a VO class that has multiple members - name, regions, id. There are duplicates in ArrayList because different regions appear multiple times. The list is sorted by ID. Here is an example: Entry 0 - Name: John Smith; Region: N; ID: 1 Entry 1 - Name: John Smith; Region: MW; ID: 1 Entry 2 - Name: John Smith; Region: S; ID: 1 Entry 3 - Name: Jane Doe; Region: NULL; ID: 2 Entry 4 - Name: Jack Black; Region: N; ID: 3 Entry 6 - Name: Jack Black; Region: MW; ID: 3 Entry 7 - Name: Joe Don; Region: NE; ID: 4 I want to transform the list to below by combining duplicate regions together for the same ID. Therefore, the final list should have only 4 distinct elements with the regions combined. So the output should look like this:- Entry 0 - Name: John Smith; Region: N,MW,S; ID: 1 Entry 1 - Name: Jane Doe; Region: NULL; ID: 2 Entry 2 - Name: Jack Black; Region: N,MW; ID: 3 Entry 3 - Name: Joe Don; Region: NE; ID: 4 What are your thoughts on the optimal way to solve this? I am not looking for actual code but ideas or tips to go about the best way to get it done. Thanks for your time!!!

    Read the article

  • ArrayList access

    - by Ricky McQuesten
    So once again I have a question about this program. I want to store transactions that are made in an arraylist and then have an option in the case menu where I can print out those that are stored. I have been researching online and have been unable to find a solution to this, so is this possible and how would I go about doing this? I also want to attach a timestamp to each transaction as well. Here is the code I have so far. So my question is how would I add a timestamp to each withdrawal or deposit, and how would I store each transaction in array list? import java.util.*; public class BankAccount extends Money { //inheritence static String name; public static int acctNum; public static double balance, amount; BankAccount(String name, int accNo, double bal) { this.name = name; this.acctNum = accNo; this.balance = bal; } void display() { System.out.println("Your Name:" + name); System.out.println("Your Account Number:" + acctNum); System.out.println("Your Current Account Balance:" + Money.getBalance()); } void displayBalance() { System.out.println("Balance:" + balance); } } import java.util.Scanner; /** * * @author Ricky */ public class Money { public static int accountNumber; public static double balance; static double amount; static String name; public void setDeposit(double amount) { balance = balance + amount; if (amount < 0) { System.out.println("Invalid"); } } public double getDeposit() { return 1; } public void setBalance(double b) { balance = b; } public static double getBalance() { return balance; } public void setWithdraw(double amount) { if (balance < amount) { System.out.println("Not enough funds."); } else if(amount < 0) { System.out.println("Invalid"); } else { balance = balance - amount; } } public double getWithdraw() { return 1; } } import java.util.*; public class Client { public static void main(String args[]) { int n = 0; int count; String trans; ArrayList<String> transaction= new ArrayList<String>(n); Scanner input = new Scanner(System.in); System.out.println("Welcome to First National Bank"); System.out.println("Please enter your name: "); String cusName = input.nextLine(); System.out.println("You will now be assigned an account number."); Random randomGenerator = new Random(); int accNo = randomGenerator.nextInt(100000); //random number System.out.println("Your account number is: " + accNo); System.out.println("Please enter your initial account balance: "); Double balance = input.nextDouble(); BankAccount b1 = new BankAccount(cusName, accNo, balance); b1.setBalance(balance); int menu; /*System.out.println("Menu"); System.out.println("1. Deposit Amount"); System.out.println("2. Withdraw Amount"); System.out.println("3. Display Information"); System.out.println("4. Exit");*/ boolean quit = false; do { System.out.println("*******Menu*******"); System.out.println("1. Deposit Amount"); // menu to take input from user System.out.println("2. Withdraw Amount"); System.out.println("3. Display Information"); System.out.println("4. Exit"); System.out.print("Please enter your choice: "); menu = input.nextInt(); switch (menu) { case 1: System.out.print("Enter depost amount:"); b1.setDeposit(input.nextDouble()); b1.getDeposit(); transaction.add(trans); break; case 2: System.out.println("Current Account Balance=" + b1.getBalance()); System.out.print("Enter withdrawal amount:"); b1.setWithdraw(input.nextDouble()); b1.getWithdraw(); transaction.add(trans); break; // switch statments to do a loop case 3: b1.display(); break; case 4: quit = true; break; } } while (!quit); } } public class Date { static Date time = new Date(); }

    Read the article

  • Java ArrayList initialization

    - by Jonathan
    I am aware that you can initialize an array during instantiation as follows: String[] names = new String[] {"Ryan", "Julie", "Bob"}; Is there a way to do the same thing with an ArrayList? Or must I add the contents individually with array.add()? Thanks, Jonathan

    Read the article

  • Java: using foreach over strings from arrayList does not print anything

    - by HH
    $ javac ArrayListTest.java $ java ArrayListTest $ cat ArrayListTest.java import java.io.*; import java.util.*; public class ArrayListTest{ public static void main(String[] args) { try { String hello ="oeoaseu oeu hsoae sthoaust hoaeut hoasntu"; ArrayList<String> appendMe = null; for(String s : hello.split(" ")) appendMe.add(s+" "); for(String s : appendMe) System.out.println(s); //WHY DOES IT NOT PRINT? }catch(Exception e){ } } }

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >