The Artima Developer Community
Sponsored Link

Java Answers Forum
Array of a vector

3 replies on 1 page. Most recent reply: Jan 22, 2003 12:56 PM by Kevin Klinemeier

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
edwin mo

Posts: 7
Nickname: ct976
Registered: Oct, 2002

Array of a vector Posted: Jan 21, 2003 10:14 PM
Reply to this message Reply
Advertisement
hi, how do you make an array of a vector?
that is.. put a new vector in each array element.

so far i have this, which reads a text file and puts it into ONE vector. but i need to create a new vector everytime the word "LINE" in the file is encountered. how do i create an array to hold a vector??


static void text2Vector (String inputFile) {

// new list of names from the file
try {
inFile = new DataInputStream (new FileInputStream (inputFile));
newVector = new Vector();
while (true) { // exits with return or break
String name = inFile.readLine();
if (name == null) // end of file
break;
name = name.trim(); // get rid of leading & trailing blanks
newVector.add(name); // add name to newLINE
} // end while
}
catch(IOException e) {
System.out.println ("I/O Error");
}
} // end text2Vector


srinivas

Posts: 6
Nickname: srinivas
Registered: Nov, 2002

Re: Array of a vector Posted: Jan 21, 2003 11:13 PM
Reply to this message Reply
HiCt

Its cool,

your problem can be solved with dynamic arrays
coz a file contain n number of lines n can vary from 1,2,.....n.

in java u can't have dynamic arrays directly

using vectors of Vectors these problem can be solved.


example

Vector array_vector = new Vector();

Vector v_lineOutput = null;

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("file name")));

String file_output =""
while((file_output=br.readLine())!=null)//till End of file
{
// file_output contain a line content

v_lineOutput = new Vector();
v_lineOutput.add(file_output);
array_vector.add(v_lineOutput);
}

fro m here u can convert array_vector in to an Array.
I hope this can be helpful to u

Regards

Srini Vallepu

edwin mo

Posts: 7
Nickname: ct976
Registered: Oct, 2002

Re: Array of a vector Posted: Jan 22, 2003 9:01 AM
Reply to this message Reply
yes, thanx. but actually i asked my question wrong i think.. i cant use a vector.. actually what i have to do is
read the text file, if the word "LINE" in the text file is encountered, substring(0,4).equals("LINE") i have to create a NEW array. so one array for each LINE. my problem is that, i don't know how to create a new array everytime it runs inside a loop?? how do u rename an array with the index??

thanx

Kevin Klinemeier

Posts: 7
Nickname: zipwow
Registered: Dec, 2002

Re: Array of a vector Posted: Jan 22, 2003 12:56 PM
Reply to this message Reply
I think I understand what you're trying to do, you want a list with an entry for each 'line' in your file. Each 'line' entry should itself be a list, with some elements for the contents of that line.

You might try something like the following:

List allLines = new ArrayList();
while (dataAvailable) {
    String newData = readline();
    
    if (newData.startswith('LINE') {
         List lineList = new ArrayList();
         addDataToLineList(); //or however you're parsing the different parts of your list
         allLines.add(lineList);
    }
}

This will create one List called 'allLines', and insert into it as many new Lists as there are 'LINE' entries in your file.

Scoping rules dictate that you won't have a named reference to any of those internal lists outside the loop, so if you want to do something with them, you'll have to iterate.

As a side note, you probably want to use ArrayLists, they're basically the same as Vectors, except they're substantially faster because they're not synchronized.

Another side note, if your 'LINE' entries are all formatted the same (like "LINE 0 name1 name2 address1 .. ect), you may want to make some little inner class that will parse the string into discrete fields, and provide accessors, and put that in the List rather than a vector.

-Kevin

Flat View: This topic has 3 replies on 1 page
Topic: Call a java methode from VB project?!?!!? Possible or not? Previous Topic   Next Topic Topic: Preloaded Servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use